0

I have a array of arrays like the following:

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

I now wish to delete the array item: ["ooooh", "that"]

Assuming that I know element 0 is "ooooh" and element 1 is "that", but i do not know the element's position within the enclosing array, can this be achieved efficiently?

The general responses from my research seem to be saying delete myArray['1'], or know the number of the element in the array- in my case in need both element 0 & 1 to match for removal.

In pseudo code i would like:

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

How can this be achieved?

10
  • 2
    myList.splice(myList.findIndex(e => e[0] === "ooooh" && e[1] === "that"), 1); Commented Jul 3, 2018 at 15:33
  • Can you assume that your delete array will be identical to the one being deleted? Commented Jul 3, 2018 at 15:33
  • 2
    Possible duplicate of javascript - remove array element on condition Commented Jul 3, 2018 at 15:35
  • 1
    A note towards my comment: if there is the possibility the element does not exist, you need to add additional code. findIndex will return -1 if it does not exist, and splice will interpret that as splice(myList.length - 1, 1). Commented Jul 3, 2018 at 15:42
  • 1
    @Pitto if you tag PM 77-1 you might get a response from him (seems like he knows the answer ) Commented Jul 5, 2018 at 13:43

7 Answers 7

3

You can use splice to remove an item out of the list.

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

Hope this helps :>

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

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

console.log(myList)

Sign up to request clarification or add additional context in comments.

Comments

2

Just filter your array

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)

Comments

2

You can use filter and every together to have it flexible.

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

Comments

2

Well there are several ways of doing this as the other answers pointed out, but I think the simplest way is to use filter. Like this::

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

console.log(myList)

Comments

1

You can use the filter function comparing arrays as strings

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

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

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

console.log(myList)

Comments

0

I'm late to the party but here is my input:


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

Comments

0

You can also use pop to remove an item out of your array.
Here's its documentation.

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

1 Comment

thanks for the response, and i'm not the downvoter, but this needs to be for a list of lists, and pop would show the last element pushed to the list no?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.