In my project I have an array, holding thousands of objects. I need to search for an explicit object inside the array. When the match is found, I need to be able to access the object properties. Because of performance I want to use Javascript’s .some() function. But with the code I have so far I only get a ‘true’ as return. How can I access the properties inside when the if-statement is a hit?
My code:
JavaScript
x
12
12
1
let array = [
2
{object.uid: 'one',
3
object.value: 'Hello one'},
4
{object.uid: 'two',
5
object.value: 'Hello two'},
6
{object.uid: 'three',
7
object.value: 'Hello three'}]
8
9
if (array.some(e => e.uid == "two")){
10
//how do I get object.value here?
11
};
12
Advertisement
Answer
You need to use find() method instead of some()
JavaScript
1
11
11
1
let array = [
2
{uid: 'one',
3
value: 'Hello one'},
4
{uid: 'two',
5
value: 'Hello two'},
6
{uid: 'three',
7
value: 'Hello three'}]
8
const obj = array.find(e => e.uid == "two");
9
if (obj){
10
console.log(obj)
11
};