bharys
bharys

Reputation: 192

How does Javascript 'includes' method work when passing an object to array of objects

Does includes method consider references too.

Eg:

let temp1 = [{a:5,b:3},{a:10,b:10}]
temp1.includes(temp1[0]) 
//returns true

let obj = {...temp1[0]}
temp1.includes(obj)
//returns false

Can someone please explain how includes method is working in this context

Upvotes: 0

Views: 654

Answers (1)

alfouz
alfouz

Reputation: 106

When you use temp1[0] you are comparing it with the exact same object, which is its first item, so it will return true.

When you uses the spread operator (...temp1) you are cloning the object, so when you compare both objects they won't be the exact same objects, and it will return false.

Upvotes: 2

Related Questions