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.

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

3
  • 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?CommentedMay 10, 2019 at 9:16
  • 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 !== undefinedCommentedMay 10, 2019 at 9:19
  • 1
    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.CommentedMay 10, 2019 at 9:21
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

    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);

      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);

        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

          Start asking to get answers

          Find the answer to your question by asking.

          Ask question

          Explore related questions

          See similar questions with these tags.