1var obj = {a: 5};
2var array = [obj, "string", 5]; // Must be same object
3array.indexOf(obj) !== -1 // True
1//Use something like this:
2
3function containsObject(obj, list) {
4 var i;
5 for (i = 0; i < list.length; i++) {
6 if (list[i] === obj) {
7 return true;
8 }
9 }
10
11 return false;
12}
13
14//In this case, containsObject(car4, carBrands) is true. Remove the carBrands.push(car4); call and it will return false instead. If you later expand to using objects to store these other car objects instead of using arrays, you could use something like this instead:
15
16function containsObject(obj, list) {
17 var x;
18 for (x in list) {
19 if (list.hasOwnProperty(x) && list[x] === obj) {
20 return true;
21 }
22 }
23
24 return false;
25}