从列表列表中删除列表

时间:2018-07-03 15:30:11

标签: javascript list

我有一个列表列表,如下所示:

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

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

假设我知道元素0是"ooooh",元素1是"that",但是我不知道元素在封闭列表中的位置,可以做到这一点而无需循环播放?

从我的研究中得出的一般答复似乎是在说“删除myList ['1']”,或者知道数组中元素的数量-在我的情况下,需要元素0和1都匹配才能删除。 / p>

我想用伪代码:

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

如何实现?

7 个答案:

答案 0 :(得分:3)

您可以使用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)

答案 1 :(得分:2)

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

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

console.log(myList)

答案 2 :(得分:1)

只需过滤您的数组

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)

答案 3 :(得分:1)

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

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

let findMe = ["ooooh", "that"]

myList = myList.filter((curr) => curr.join() !== findMe.join())

console.log(myList)

答案 4 :(得分:1)

您可以同时使用过滤器和每个过滤器以使其更加灵活。

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);

答案 5 :(得分:0)

您还可以使用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);

答案 6 :(得分:0)

我参加聚会很晚,但这是我的投入:


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);