2

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
asked May 10, 2019 at 9:07
4
  • 1
    It's 'hobbies', just in case Commented May 10, 2019 at 9:11
  • users.some(user => user.hobbies.first); Commented May 10, 2019 at 9:13
  • @DennisVash That will throw if user.hobbies does not exist Commented May 10, 2019 at 9:22
  • 2
    @CertainPerformance users.some(user => user.hobbies && user.hobbies.first); ;-) Commented May 10, 2019 at 9:25

5 Answers 5

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

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?
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 !== undefined
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.
1

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

Comments

0

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

Comments

0

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

Comments

0

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

answered May 10, 2019 at 9:35

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.