2

I want to delete an element that in this case is an array within another array

arr = [['a','b'],['c','d'],['e','f']];
tag = ['c','d'];

I want to remove tag from arr, and for that I'm trying to do this:

arr.splice(arr.indexOf(tag), 1);

But I do not know why it does not work, How could I do this?

3
  • Thats because indexOf can't find tag. Don't think you can use an array as an argument for it. Commented Jun 23, 2018 at 19:22
  • indexOf uses strict equality. Different arrays are not strictly equal because they're different objects, even if they contain the same elements. You'd need to use a filter and either manually or via a library do an array compare (and decide other things like if order is important etc). Commented Jun 23, 2018 at 19:23
  • 1
    If tag is equal to ['c','e'] it will fail and remove the second element Commented Jun 23, 2018 at 19:25

5 Answers 5

3

It doesn't work because [1, 2, 3] != [1, 2, 3] in javascript. Arrays and object comparisons are not done by the values. This also applies to indexOf().

You need to tell javascript what you mean by equality:

arr = [['a','b'],['c','d'],['e','f']];
tag = ['c','d'];

function array_equals(a, b){
    return a.length === b.length && a.every((item,idx) => item === b[idx])
}

console.log(arr.filter(item => !array_equals(item, tag)))
  

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

Comments

1

You need to check every item in the arrays, because with Array#includes, you check the object reference, which is not the same, even with same values.

Assuming the same length of the arrays for checking.

var arr = [['a', 'b'], ['c', 'd'], ['e', 'f']],
    tag = ['c','d'],
    result = arr.filter(a => !a.every((v, i) => v === tag[i]));

console.log(result);

1 Comment

This filters out ['c', 'd'] when tag = ['c','d', 'e']
0

You will need to use a doble filter. First to separate the arrays and second will be the one comparing each value and will filter out the tag element.

arr = [['a','b'],['c','d'],['e','f']];
tag = ['c','d'];


console.log(arr.filter(el=> el.filter((c,i)=> c != tag[i]).length != 0))

Comments

0

In a lazyer alternative, the arrays can be converted and compared as strings if none of the values contains , :

var arr = [['a', 'b'], ['c', 'd'], ['e', 'f']], tag = ['c', 'd'];

console.log( arr.filter(a => a != tag + '') )

Comments

0

Here is a filter function which removes number 5 from an array of number arrays:

let winComb = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
  [1, 4, 7],
  [2, 5, 8],
  [3, 6, 9],
  [1, 5, 9],
  [3, 5, 7],
];

for (let i = 0; i < winComb.length; i++) {
  for (let j = 0; j < 3; j++) {
    newArr = winComb[i].filter(function (item) {
      return item !== 5;
    });
  }
  console.log(newArr);
}

Comments

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.