0

Could someone guide me how to find an element in an array?

I always get false for elements that exist.

Thank you.

arr = new Array;
arr.push({x: "a", y: 1});
arr.push({x: "b", y: 2});
arr.push({x: "c", y: 3});
​
elem = {x: "c", y: 3};
​
if (arr.includes(elem)){
 console.log('true');
} else {
 console.log('false');
}
crashmstr
28.6k9 gold badges66 silver badges80 bronze badges
asked Sep 20, 2021 at 0:47
2
  • 1
    JSON is "JavaScript Object Notation". JSON is a string representation of JavaScript Objects. There is no JSON here. Commented Sep 20, 2021 at 1:01
  • sorry for my mistake. Commented Sep 20, 2021 at 1:14

2 Answers 2

3

You are looking for {x: "c", y: 3} object in arr and It will never be equal to any other object because {} !== {}.

Remember: Object are compared with reference but the primitives are checked by their value.

In your code you are checking an object reference with another reference, which is not equal so that's why it logs false.

If there are array of numbers then you should use includes as:

const arr = [10, 20, 30];
if(arr.includes(20)) console.log("true");
else console.log("false");

You can use find to find the element in the array

const arr = new Array();
arr.push({ x: "a", y: 1 });
arr.push({ x: "b", y: 2 });
arr.push({ x: "c", y: 3 });
const elem = { x: "c", y: 3 };
if (arr.find((o) => o.x === elem.x && o.y === elem.y)) {
 console.log("true");
} else {
 console.log("false");
}

answered Sep 20, 2021 at 0:49

10 Comments

Thanks for your quick and wonderful help, is there another simpler way to search or is this the only option?
This is the most direct and easiest method to check existence in an array of objects.
Excellent, thank you so much, have a wonderful day, best regards.
A tangential note: find returns undefined or the element that is found. The found element is likely to be truthy, but generally you use find to get the element and could use .some to just test for existence. Array.prototype.some vs. Array.prototype.find
@HR01M8055 .find will return the found element or undefined (a falsy value, but not false). You can also contrive an example where it returns undefined when undefined is a value in the array (so there exists a value that matches the predicate and .some would return true but .find returns undefined). My point is that the choice is informative to the reader of the code as to what is wanted out of it: Do you want to check for any matching, get the first matching, get all matching, or check if all match? Use the tool that matches intent most closely.
|
1

since you are working with a 2d array i believe your object will be allways same for X and Y , so i suggest you to make a function like that for further use in your code

arr = new Array;
arr.push({x: "a", y: 1});
arr.push({x: "b", y: 2});
arr.push({x: "c", y: 3});
elem = {x: "c", y: 3};
console.log(findXY(arr,elem))
function findXY(arr,val){
 return arr.find(each=>{
 return each.x == val.x && each.y == val.y
 })
}

answered Sep 20, 2021 at 2:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.