I want to check if a object value exist in a array of objects with "some".
I only can find that the object hobbys exist in the array with "some"
"users": [
{
"type": "User",
"name": "Sarah",
"hobbys":
{
first: "something1",
second: "something2"
}
}
]
users.some(item => item.hobbys); // true
I want to check if "first" value in "hobbys" exist.
Nikhil Aggarwal
28.5k4 gold badges46 silver badges61 bronze badges
5 Answers 5
Check if the hobbys is an object and has an own property of first:
const users = [{
"type": "User",
"name": "Sarah",
"hobbys": {
first: "something1",
second: "something2"
}
}];
const userWithFirstHobbyExists = users.some(({ hobbys }) => (
typeof hobbys === 'object' && hobbys.hasOwnProperty('first')
));
console.log(userWithFirstHobbyExists);
answered May 10, 2019 at 9:08
CertainPerformance
374k55 gold badges354 silver badges359 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Yevhen Horbunkov
To me
hasOwnProperty() is a tool for tracking down the inheritance, which is muuuuch slower than simple users.some(item => item.hobbys.first != undefined || false). What's the benefit of using hasOwnProperty() here?CertainPerformance
hasOwnProperty is the way to check to see if the property exists on the object: != undefined will also evaluate to false if the property does exist, but its value is undefined. It's an edge case, but from OP's question, it's not clear if it's something to worry about or not. If he also wants to exclude first values of undefined, then you're right, check !== undefinedYevhen Horbunkov
The question body states "want to check if "first" value in "hobbys" exist", so regardless of the
first value, hasOwnProperty() seems to be massive overkill.You can try following assuming hobbys will always be an object and first will not have any falsy values.
let users = [{"type": "User","name": "Sarah","hobbys": {"first": "something1","second": "something2"}}];
console.log(users.some(item => item.hobbys && item.hobbys.first)); // true
answered May 10, 2019 at 9:12
Nikhil Aggarwal
28.5k4 gold badges46 silver badges61 bronze badges
Comments
You can use "key" in obj also to check if a key is part if object.
const users = [{
"type": "User",
"name": "Sarah",
"hobbys": {
first: "something1",
second: "something2"
}
}];
const userWithFirstHobbyExists = users.some(({ hobbys }) => (
typeof hobbys === 'object' && ('first' in hobbys )
));
console.log(userWithFirstHobbyExists);
Alternate and fast way to check is
const users = [{
"type": "User",
"name": "Sarah",
"hobbys": {
first: "something1",
second: "something2"
}
}];
const userWithFirstHobbyExists = users.some(({ hobbys }) => (
typeof hobbys === 'object' && hobbys['first'] !== undefined
));
console.log(userWithFirstHobbyExists);
answered May 10, 2019 at 9:11
Kaushik
2,0901 gold badge24 silver badges32 bronze badges
Comments
const users = [{
"type": "User",
"name": "Sarah",
"hobbys": {
first: "somethig1",
second: "something2"
}
}];
const hobbyExists = users.some(({ hobbys }) => (typeof hobbys === 'object' && hobbys['first']));
console.log(hobbyExists);
answered May 10, 2019 at 9:20
Manikandan Velayutham
2,2281 gold badge17 silver badges22 bronze badges
Comments
function will return true if it has value else false
let jData = {
"users": [{
"type": "User",
"name": "Sarah",
"hobbys": {
"first": "something1",
"second": "something2"
}
}]
};
function hasValue(data, obj, key) {
return data.some(item => item[obj] && item[obj][key]);
}
console.log(hasValue(jData.users, 'hobbys', 'second')) //true
console.log(hasValue(jData.users, 'hobbys', 'first')) // true
console.log(hasValue(jData.users, 'test', 'first')) //false
Comments
lang-js
users.some(user => user.hobbies.first);user.hobbiesdoes not exist