bzlight
bzlight

Reputation: 1150

Javascript find nested object by value

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?

Upvotes: 2

Views: 602

Answers (3)

ajai Jothi
ajai Jothi

Reputation: 2294

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

Upvotes: 0

Min Lee
Min Lee

Reputation: 161

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".

Upvotes: 1

JonoJames
JonoJames

Reputation: 1223

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

Upvotes: 1

Related Questions