0

I have an array of objects and use indexOf to find a specific object in that array.

I have switched to an online datastore and because the data for a selected object is now returned as variables I have to create a "dummy" object that is then used to look up the matching selected object in the array.

I have compared the dummy object and the object in the array and they look identical however null is always returned.

This is the array of objects:

data:
[ 
    Creative {
        id: '5631943370604544',
        gid: '5631943370604544',
        name: 'Hello',
        description: '',
        url: '',
        videos: [] 
    },
    Creative {
        id: '5653294995210240',
        gid: '5653294995210240',
        name: 'Yo',
        description: '',
        url: '',
        videos: []
    },
    Creative {
        id: '5745189578604544',
        gid: '5745189578604544',
        name: 'Sup',
        description: '',
        url: '',
        videos: [] 
    } 
]

This is the dummy object:

object:

Creative {
    id: '5653294995210240',
    gid: '5653294995210240',
    name: 'Yo',
    description: '',
    url: '',
    videos: [] 
}

Here is the function that is used for the look up. This is unmodifiable as it is part of GraphQL and Relay and it is the function that is used.

function cursorForObjectInConnection(data, object) {
  var offset = data.indexOf(object);
  if (offset === -1) {
    return null;
  }
  return offsetToCursor(offset);
}
BennyTicklez
  • 147
  • 1
  • 18

2 Answers2

1

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

from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

This is a very important rule to know in Javascript. Your dummy object (even though it looks identical) is not the same as the object in that array. They exist in two separate locations in your computer's memory, thus the strict equality comparison used by indexOf considers them not equal.

This would not be the case for primitive data types likes strings, numbers, or booleans.

jlogan
  • 947
  • 6
  • 9
0

why you don't make your own find function (search By id) and Search for object By ID ??!!