0

I am trying to do a very simple indexOf search without success.

I have a two dimensional Array like this:

var fruits = new Array([]);
fruits.push(["Banana", "Orange", "Apple", "Mango"]);
fruits.push(["Apple", "3Orange", "Amar", "Mango"]);
fruits.push(["Apple", "1Orange", "Amar", "Mango"]);
fruits.push(["Apple", "2Orange", "Amar", "Mango"]);

Now I am creating another Array as below which matches the third record of the above array:

var str = new Array([]);
str.push(["Apple", "2Orange", "Amar", "Mango"]);

Now I try to find if str exists in fruits:

var i  = fruits.indexOf( str );
alert(i);

But I returns -1 instead of a valid index value. What am I doing wrong?

Edit

I have figured out a very simpler way. Here is what appears to also work:

var strFruits = fruits.toString();
var newStr = str.toString();
var i  = fruits.indexOf( str );
alert(i);

This obviously has a pitfall of finding matching value across two records. But in my case I know that won't be possible because of the nature of data set that I am using. Not a good practice as a general solution but in specific cases it might be handy.

halfer
  • 19,824
  • 17
  • 99
  • 186
AnR
  • 1,809
  • 3
  • 26
  • 45

2 Answers2

1

If you pass an object to Array.prototype.indexOf(), then its reference will be verified not its values. So you have to write a code like this to achieve what you want,

var str = new Array([]);
str.push(["Apple", "2Orange", "Amar", "Mango"]);
var i  = checkIt(fruits, str[0]);
alert(i);

function checkIt(src, arr){
  arr = arr.toString();
  var ind = -1;
  src.forEach(function(itm,i){
    if(itm.toString() == arr){
      ind = i;
      return;
    }
  });
  return ind;
}

DEMO

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
  • "Array.prototype.indexOf() cannot accept an object as its parameter" is not correct. `var x = {};` `console.log([x].indexOf(x));` – Evan Trimboli Feb 26 '16 at 12:12
  • @EvanTrimboli Of course yes, but that is not a good practice to use so. That is what i mentioned in that way. Let me correct the post now. – Rajaprabhu Aravindasamy Feb 26 '16 at 12:14
  • @RajaprabhuAravindasamy Do I really need str[0] in checkIt(fruits, str[0]);? Won't just str be just good enough? – AnR Feb 26 '16 at 12:47
  • @Anjum That would be enough, but it will ruin the readability as it is a multidimensional array. – Rajaprabhu Aravindasamy Feb 26 '16 at 12:48
  • Actually str is actually a single dimensional array as it contains a single record. We can even use this notation I believe str = ["Apple", "2Orange", "Amar", "Mango"]; in which case [0] obviously won't be valid. But overall I agree. Thanks – AnR Feb 26 '16 at 13:04
  • @Anjum But this `var str = new Array([]); str.push(["Apple", "2Orange", "Amar", "Mango"]);` code of yours made me to think it is a multidimensional array. Anyway glad to help. :) – Rajaprabhu Aravindasamy Feb 26 '16 at 13:06
  • Not good practice? What if you need to find some object reference? – Evan Trimboli Feb 26 '16 at 13:31
  • @EvanTrimboli Using `eval()` is being considered as a bad practice in some context. But if you use it in the way that it suppose to be used, then it would yield good results. Like wise, if people use `.indexOf()` in the way our OP thinks(checks the value of the object) then it would not give expected results. Anyway practically, chances of checking a reference in an array wont raise maximum for 99%. If raises as you said then it can be used with its behaviour in mind. – Rajaprabhu Aravindasamy Feb 26 '16 at 13:43
1
var arr1 = [1,2,3]; 
var arr2 = [1,2,3];

console.log(arr1 == arr2)   //false
console.log(arr1 === arr2)  //false

and from MDN

indexOf() compares searchElement to elements of the Array using strict equality (the same method used by the ===, or triple-equals, operator).

and 2 objects, even though with same internal properties, wont match.

so to match, you need to compare the array by elements inside it to match it

to compare the elements in the array, you can follow another question in SO, LINK

Community
  • 1
  • 1
Oxi
  • 2,918
  • 17
  • 28