0

If i write a indexOf function for json array always it's returning -1.

parent = [{'a':'hello'},{'a':'world'}];
var child = {'a':'hello'};
var index = parent.indexOf(child);

actually this index should be 0 but it's returning -1;

PLease help me with this issue.

Webfreaks
  • 113
  • 3
  • 7
  • 1
    `indexOf` can't compare objects like this. – Cerbrus Jun 17 '14 at 12:44
  • I think your answer is here: http://stackoverflow.com/questions/1068834/object-comparison-in-javascript – TuanDT Jun 17 '14 at 13:02
  • 1
    Bear in mind that when using the `{...}` notation, you are implicitly creating a **new** object, which has nothing in common with the object you previously defined, even if it has the same properties & values. For example, try `alert({'a':'b'} == {'a' : 'b'})` in JSFiddle... If you want to look for the very object you pushed into parent, you have to pass its reference to the `indexOf` method. For example, `var child = {'a':'hello'}; parent = [child,{'a':'world'}]; var index = parent.indexOf(child);` – Waldo Jeffers Jun 17 '14 at 13:06

0 Answers0