【问题标题】:Remove element from Array of Arrays从数组数组中删除元素
【发布时间】:2018-07-03 15:30:11
【问题描述】:

我有一个数组,如下所示:

myArray = [
  ["ooooh", "that"],
  ["dog ", "of"],
  ["mine", "word...."]
]

我现在想删除数组项:["ooooh", "that"]

假设我知道元素 0 是"ooooh",元素 1 是 "that",但是我不知道元素在封闭数组中的位置,这可以有效地实现吗?

我研究的一般反应似乎是delete myArray['1'],或者知道数组中元素的数量——在我的例子中,需要元素 0 和 1 来匹配删除。

我想用伪代码:

myArray.destroy(["ooooh", "that"])

如何做到这一点?

【问题讨论】:

  • myList.splice(myList.findIndex(e => e[0] === "ooooh" && e[1] === "that"), 1);
  • 你能假设你的删除数组与​​被删除的数组相同吗?
  • 对我的评论的注释:如果元素可能不存在,您需要添加额外的代码。 findIndex 将返回 -1 如果它不存在,splice 将解释为 splice(myList.length - 1, 1)
  • @Pitto 如果您标记 PM 77-1,您可能会收到他的回复(似乎他知道答案)

标签: javascript arrays


【解决方案1】:

您可以使用splice 将项目从列表中删除。

myList.splice(
   myList.findIndex( item => 
     item[0] == "ooooh" && item[1] == "that"
   ), 
1);

希望这会有所帮助:>

myList = [
["ooooh", "that"],
["dog ", "of"],
["mine", "word...."]];

myList.splice(myList.findIndex(item => item[0] == "ooooh" && item[1] == "that"), 1);

console.log(myList)

【讨论】:

    【解决方案2】:

    正如其他答案指出的那样,有几种方法可以做到这一点,但我认为最简单的方法是使用过滤器。像这样::

    myList = [
        ["ooooh", "that"],
        ["dog ", "of"],
        ["mine", "word"]
    ];
        
    myList = myList.filter((element)=>
        //filter condition 
        (!
           ((element[0] == 'ooooh') && (element[1] == 'that'))
        )
    );
    
    console.log(myList)

    【讨论】:

      【解决方案3】:

      只过滤你的数组

      var myList = [
      ["ooooh", "that"],
      ["dog ", "of"],
      ["mine", "word...."]
      ]
      
      deleteFromList = (val, list) => list.filter(a=>JSON.stringify(a)!=JSON.stringify(val))
      
      console.log(
        deleteFromList(["ooooh", "that"], myList)
      )
      
      /* Same as */
      
      myList = myList.filter(a=>JSON.stringify(a)!=JSON.stringify(["ooooh", "that"]))
      
      console.log(myList)

      【讨论】:

        【解决方案4】:

        您可以使用filter 函数将数组作为字符串进行比较

        myList = [
          ["ooooh", "that"],
          ["dog ", "of"],
          ["mine", "word...."]
        ];
        
        let findMe = ["ooooh", "that"]
        
        myList = myList.filter((curr) => curr.join() !== findMe.join())
        
        console.log(myList)

        【讨论】:

          【解决方案5】:

          您可以将 filter 和every 一起使用以使其灵活。

          const myList = [
            ["ooooh", "that"],
            ["dog ", "of"],
            ["mine", "word...."]
          ];
          
          const toDelete = ["ooooh", "that"];
          
          const res = myList.filter(e => ! toDelete.every(x => e.includes(x)));
          console.log(res);

          【讨论】:

            【解决方案6】:

            我迟到了,但这是我的意见:


            let myList = [
              ["ooooh", "that"],
              ["dog ", "of"],
              ["mine", "word...."]
            ];
            
            Array.prototype.destroy = function(array) {
              this.forEach((el, i) => {
                if (JSON.stringify(el) === JSON.stringify(array)) {
                  this.splice(i, 1);
                }
              });
            }
            
            console.log(myList);
            
            myList.destroy(["ooooh", "that"]);
            
            console.log(myList);

            【讨论】:

              【解决方案7】:

              您还可以使用 pop 从数组中删除一个项目。
              Here 的文档。

              myArray = [
              ["ooooh", "that"],
              ["dog ", "of"],
              ["mine", "word..."]
              ]
              
              console.log("Original Array:");
              console.log(myArray);
              
              /**
               * Removes one array from an array of arrays 
               *
               * @param  {String} myArray       - The original array of arrays
               * @param  {String} arrayToRemove - The array that will be removed from myArray
               */
              function destroy(myArray, arrayToRemove) {
                      arrayIndexToBeRemoved = myArray.findIndex(item => item[0] == arrayToRemove[0] && item[1] == arrayToRemove[1]);
                      if (arrayIndexToBeRemoved != -1){
                          removedArray = myArray.pop(arrayIndexToBeRemoved);
                          console.log("Removed: " + removedArray);
                      } else {
                          console.log("Couldn't find: " + arrayToRemove + " in: " + myArray);
                      }
              }
              
              destroy(myArray, ["mine", "word..."]);
              
              console.log("Final Array:");
              console.log(myArray);

              【讨论】:

              • 感谢您的回复,我不是反对者,但这需要一个列表列表,并且 pop 会显示最后一个推送到列表的元素吗?
              猜你喜欢
              • 2015-06-12
              • 2011-10-31
              • 2020-04-28
              • 2022-01-13
              • 2016-03-08
              • 2017-09-13
              • 2017-12-22
              • 1970-01-01
              相关资源
              最近更新 更多