0

This is my function to delete unwanted array value.

 function deleteValue(array1,array2) {

  var len =  array2.length;
  for (var i=0; i<len;i++) {
    if (array2[i] ==array1)
    { array2.splice(i,1)}
}
    return array2;
};

But it doesnt work.

My two array look like this:

array1 is an array inside a nested array (array2)

array1 = [1,2,3,4];

array2 = [[1,2,3,4],[5,6,7,8]];

array1 looks the same as array2[0]

But if I Boolean(array1==array2[0]) it always return false.

Is it the type problem?

Shades
  • 5,568
  • 7
  • 30
  • 48
Frostless
  • 818
  • 1
  • 11
  • 33
  • Proper spacing/indentation always helps. If you're editor has a "format file" function, give it a try – qxz Feb 18 '17 at 01:13
  • First of all, `===` should almost always be used in favor of `==`. Second, `==` only makes a shallow comparison with objects. It gives reference equality, not value equality. – 4castle Feb 18 '17 at 01:15
  • `==` with arrays (and other objects) only results in `true` if the two arrays are _the same array_ – if you give it two different arrays, it will return `false`, *even if the two arrays have the same content*. To check if they have the same content, you'll need to write a function to check each element. – qxz Feb 18 '17 at 01:17
  • @squint Marked it as a duplicate of another duplicate? .... – qxz Feb 18 '17 at 01:19
  • @qxz: Yes. Why do you ask? –  Feb 18 '17 at 01:21
  • @squint It seems like you'd want to link to a good question with complete answers, not just another duplicate. – qxz Feb 18 '17 at 04:33
  • @qxz: Just because it's a duplicate, doesn't mean it's not a good question with complete answers. And by linking to a duplicate, another link is present to follow for more information. In short, there's no inherent reason to not do it. –  Feb 18 '17 at 13:10

0 Answers0