1

I am trying to check if an object is already in an array, following this answer here: How to determine if object is in array

I adjusted the function to suit my needs, and now it looks like this:

var _createDatesArray, _objInArray;

_objInArray = function(array, obj) {
  var i;
  i = 0;
  while (i < array.length) {
    console.log("array[i] == obj is ", array[i] === obj, " array[i] is ", array[i], " and obj is ", obj);
    if (array[i] === obj) {
      return true;
    }
    i++;
  }
};

_createDatesArray = function(val) {
  var result;
  if (val != null) {
    result = {
      text: val
    };
    if (!_objInArray(scope.datesQuestion.dates, result)) {
      scope.datesQuestion.dates.push(result);
    }
    return console.log(scope.datesQuestion.dates);
  }
};

What I need to do, is basically see if the object is already in the array, and if is,t return true.

When debugging, the result of the console log is the following:

array[i] == obj is false array[i] is {text: "10/08/17"} and obj is {text: "10/08/17"}

and the function says they are different (array[i] == obj is false) but they look the same to me.

I also checked the type of both, which is this:

typeof array[i] is "object"
typeof obj is "object"

can you help me with this? Why are they different? what can be different?

_createDatesArray is called when $scope of my angular app changes its value based on a ng-model, but I don't think this is relevant

Joe Clay
  • 33,401
  • 4
  • 85
  • 85
Nick
  • 13,493
  • 8
  • 51
  • 98
  • Two object are equal only if they are the same object (different references). Check `{a: 'a'} === {a: 'a'}` => false. – dfsq Aug 10 '17 at 10:42

4 Answers4

3

They're two different objects with the same content. Comparing them with == or === will yield false.

Since you're using AngularJS, you can use angular.equals() instead to perform a deep comparison of the object's properties.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
1

The objects you are comparing don't have the same reference, so == is returning false. See Object comparison in JavaScript for a more detailed explanation.

In this particular case, you could simply compare the text of dates to see if they are equivilant. However this wouldn't work for all objects like the function name suggests.

if (arr[i].text === obj.text)

Alternatively, you could create a method specific for checking if your array includes a given date and simplify it greatly using Array.prototype.some:

dateInArray = function (array, date) {
  return array.some(function (arrayDate) {
    return arrayDate.text === date.text
  })
}

Or, more succinctly using ES6 arrow functions:

dateInArray = (array, date) => array.some(arrayDate => arrayDate.text === date.text)
Craig Ayre
  • 1,133
  • 9
  • 12
0

array[i] === obj will return true ONLY if its the same object. In the link that you have referred the object being checked is the same object that is inserted in the array, thats why it returns true. In your case, you are creating a new object 'result' and adding the value in there. So the array does not contain the exact same object and hence returns false. If 'text' is the only property in the object, instead of checking for the entire object you could check if the 'text' property in both the objects is same.

_objInArray = function(array, obj) {
  var i;
  i = 0;
  while (i < array.length) {
    if (array[i].text === obj.text) {
      return true;
    }
    i++;
  }
};
jsdev
  • 101
  • 7
0

This happens because objects in JS are compared by reference, but not by values they have. But you need to compare objects by their value. So you need to get some third-party function or to write your own. One more option is to use angular built-in equals function.

angular.equals($scope.user1, $scope.user2);

For a better understanding you can read a good article on this subject here.

Roman Osypov
  • 1,321
  • 12
  • 10