1

i'm trying to use includes to see if an object is inside the array like so:

arr=[{name:'Dan',id:2}]

and I want to check like so:

arr.includes({name:'Dan',id:2})

and this returns false, is there a way to do that?

Alexander
  • 1,288
  • 5
  • 19
  • 38
  • 1
    It might sound surprising, but In javascript, these two objects are NOT equal. So you have to have a function that accepts two objects and tells true when _you_ think they're the same. – georg Dec 12 '18 at 17:50

3 Answers3

8

does include works with array of objects?

Yes — if you use the same object as the argument to includes that's in the array:

const obj = {name: "Dan", id: 2};
const arr = [{name: "T.J.", id: 42}, obj, {name: "Joe", id: 3}];
console.log(arr.includes(obj));                  // true
console.log(arr.includes({name: "Dan", id: 2})); // false

includes uses a === check, and o1 === o2 is only ever true when o1 and o2 refer to the same object. In your example, you're using two different but equivalent objects instead.

For your use case, you probably want some, which lets you execute a callback to determine whether an entry in the array matches a condition:

if (arr.some(e => e.id === 2)) {

Example:

const obj = {name: "Dan", id: 2};
const arr = [{name: "T.J.", id: 42}, obj, {name: "Joe", id: 3}];
console.log(arr.some(obj => obj.id === 2));      // true

There are various ways to spin that, depending on what you want the check to be (which are discussed at length in this question's answers), but that just means adjusting the contents of the function you pass some.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Wow, this is better. `:)` I had the same question. By reference vs Value right? – Praveen Kumar Purushothaman Dec 12 '18 at 17:53
  • 2
    @PraveenKumarPurushothaman - The check in `includes` is `===` (which I should have said above, and will :-)). `o1 === o2` is only ever true when `o1` and `o2` both contain references to the same object. (Variables hold object references, which are values. Though there are other interpretations...) – T.J. Crowder Dec 12 '18 at 17:55
  • 1
    @PraveenKumarPurushothaman: "by reference" and "by value" mean something different: https://en.wikipedia.org/wiki/Evaluation_strategy . It's not applicable here. – Felix Kling Dec 12 '18 at 18:09
  • @FelixKling Oh okay... Are Object references not the "by reference" then? – Praveen Kumar Purushothaman Dec 12 '18 at 18:10
  • @FelixKling - :-) I chose to skirt around that, and probably shouldn't have. Good link. – T.J. Crowder Dec 12 '18 at 18:11
  • 1
    @PraveenKumarPurushothaman - No, it's just the word "reference" being used for different things (a reference to an *object* is not the same thing as a reference to a *variable*, which is what "by reference" relates to). – T.J. Crowder Dec 12 '18 at 18:11
  • 1
    @PraveenKumarPurushothaman: In a nutshell: "* by reference/value" describes the relationship between variables/parameters. It has nothing to do with the actual values involved. Values can either be "reference-type" values (like object) or "value-type" values (primitives): https://en.wikipedia.org/wiki/Value_type_and_reference_type . So, JavaScript is a "* by value" language which implements objects as reference-type values. – Felix Kling Dec 12 '18 at 18:16
2

Not like that. Use some instead:

arr.some(obj => obj.id === 2)

Which checks if there is an object with id equals to 2.

If you are checking for both id and name then:

arr.some(obj => obj.id === 2 && obj.name === "Dan")
ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
2

Arrays do have includes() function, but for your case, you have to do using some():

arr.some(e => e.id === 2 && e.name === "Dan")
arr.some(e => e.name === "Dan")
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252