2

Summary of Problem

I'm still learning Javascript and I know this may be basic but I'm having some trouble.

I have a object containing nested objects and I need to check if any of the nested objects have a property that matches a specific value.

Code

I want to check the object below to see if const eSportsUsername = "Dark" exists and return a Boolean.


Const object = {
   Dark: {_id: "5da78b305f0cc7fc44417821", online: false, eSportsUsername: "Dark"},
   HighDistortion: {_id: "5da78b505f0cc7fc44417825", online: false, eSportsUsername: "HighDistortion"}
}

Can anyone recommend how to achieve this?

4
  • you could use obj.hasOwnProperty('a') if exist only you need to check if online is true Commented Nov 21, 2019 at 23:33
  • As long as your object is not overwhelmingly large, recursion is the way to go. Have you given this approach a try? Commented Nov 21, 2019 at 23:34
  • I need to check if eSportsUsername equals a specific string, eg eSportsUsername: 'Dark'. Will that still work? Commented Nov 21, 2019 at 23:34
  • @Andrew, I have tried that, how would I do that? The object has 2 to 100 objects inside of it Commented Nov 21, 2019 at 23:35

3 Answers 3

1

You have two options to get the values. dot notation as per the example or bracket notation

const object = {
   Dark: {_id: "5da78b305f0cc7fc44417821", online: false, eSportsUsername: "Dark"},
   HighDistortion: {_id: "5da78b505f0cc7fc44417825", online: false, eSportsUsername: "HighDistortion"}
}

console.log(object['Dark']['eSportsUsername']);
//or
console.log(object.Dark.eSportsUsername);

//to return a boolean 
 let boolean = '';
if(object.Dark.eSportsUsername == "Dark")
{
boolean =  true;

}else {
boolean = false;
}

console.log(boolean);

Sign up to request clarification or add additional context in comments.

1 Comment

I believe OP is referring to an object of arbitrary nesting
1

Answer

Object.values(object).some(obj => obj.eSportsUsername === "Dark") // will return true if any of the nested object has property with "Dark"

Explanation

In order to do this, you should

  • first: convert the object into array of property values
  • second: iterate through the array to check if an element has {eSportsUsername: "Dark"}

First step is achieved using Object.values(). In your example, Object.values(object) will return

[{eSportsUsername: "Dark", online: false}, {eSportsUsername:"HighDistortion", online: false}]

For the second step, we can use Array.prototype.some() to iterate the array and check if any nested object has eSportsUsername value as "Dark".

Comments

0

Used recursion to match value in nested object. This will work with and without key.

Just post in comment if you want a solution without recursion

ref: Array.prototype.entires and Array .prototype.some

const input = {
  Dark: {
    _id: "5da78b305f0cc7fc44417821",
    online: false,
    eSportsUsername: "Dark"
  },
  HighDistortion: {
    _id: "5da78b505f0cc7fc44417825",
    online: false,
    eSportsUsername: "HighDistortion"
  }
};

const isExist = (obj, val, key) => Object.entries(obj)
  .some(([k, v]) => {
    if (v instanceof Object) { 
      // [] instanceof Object -> true
      // {} instanceof Object -> true
      // "abc" instanceof Object -> false
      // 123 instanceof Object -> false
      // null instanceof Object -> false
      // undefined instanceof Object -> false
      
      return isExist(v, val, key);
    }

    const keyMatched = !key || k === key;

    return keyMatched && (v === val);
  });


console.log(isExist(input, 'Dark', 'eSportsUsername'));
console.log(isExist(input, 'HighDistortion', 'eSportsUsername'));
console.log(isExist(input, 'Dark', 'unknown'));
console.log(isExist(input, '5da78b305f0cc7fc44417821'));

1 Comment

be careful with typeof v === 'object'. typeof null is object

Your Answer

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.