代码之家  ›  专栏  ›  技术社区  ›  Preston

从列表中删除列表

  •  -1
  • Preston  · 技术社区  · 7 年前

    我有如下清单:

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

    我现在要删除列表项: ["ooooh", "that"]

    假设我知道元素0是 "ooooh" 元素1是 "that" ,但是 我不知道元素在所附列表中的位置 ,这是否可以在没有循环的情况下实现?

    我的研究得出的一般回答似乎是“delete mylist['1']”,或者知道数组中元素的数量——在我的情况下,需要元素0&1都匹配才能删除。

    在伪代码中,我希望:

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

    如何才能做到这一点?

    7 回复  |  直到 7 年前
        1
  •  3
  •   Gerardo BLANCO    7 年前

    你可以用 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
  •  2
  •   Nelson Teixeira    7 年前

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

    myList = [
        ["ooooh", "that"],
        ["dog ", "of"],
        ["mine", "word"]
    ];
        
    myList = myList.filter((element)=>
        //filter condition 
        (!
           ((element[0] == 'ooooh') && (element[1] == 'that'))
        )
    );
    
    console.log(myList)
        3
  •  1
  •   Luis felipe De jesus Munoz    7 年前

    只需过滤数组

    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
  •  1
  •   Vanojx1    7 年前

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

    myList = [
      ["ooooh", "that"],
      ["dog ", "of"],
      ["mine", "word...."]
    ];
    
    let findMe = ["ooooh", "that"]
    
    myList = myList.filter((curr) => curr.join() !== findMe.join())
    
    console.log(myList)
        5
  •  1
  •   baao    7 年前

    你可以使用过滤器和每一个一起有它的灵活性。

    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
  •  0
  •   Alex    7 年前

    我来晚了,但我的意见是:


    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
  •  0
  •   Pitto    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);
    推荐文章