简体   繁体   English

Javascript 按值查找嵌套的 object

[英]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.我仍在学习 Javascript,我知道这可能是基本的,但我遇到了一些麻烦。

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.我有一个包含嵌套对象的 object,我需要检查是否有任何嵌套对象具有与特定值匹配的属性。

Code代码

I want to check the object below to see if const eSportsUsername = "Dark" exists and return a Boolean.我想检查下面的 object 以查看 const eSportsUsername = "Dark" 是否存在并返回 Boolean。


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

Can anyone recommend how to achieve this?谁能推荐如何实现这一目标?

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

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首先:将 object 转换为属性值数组
  • second: iterate through the array to check if an element has {eSportsUsername: "Dark"}第二:遍历数组以检查元素是否具有 {eSportsUsername: "Dark"}

First step is achieved using Object.values() .第一步是使用Object.values()实现的。 In your example, Object.values(object) will return在您的示例中, Object.values(object)将返回

[{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" .第二步,我们可以使用Array.prototype.some()来迭代数组并检查是否有任何嵌套的 object 的eSportsUsername值为"Dark"

You can create a function by passing the object, key name and the value.您可以通过传递 object、键名和值来创建 function。 If the key does not exist then return false otherwise return the boolean value by comparing the key value with the passed value.如果键不存在,则返回false ,否则通过将键值与传递的值进行比较,返回 boolean 值。

 const object_data = { Dark: {_id: "5da78b305f0cc7fc44417821", online: false, eSportsUsername: "Dark"}, HighDistortion: {_id: "5da78b505f0cc7fc44417825", online: false, eSportsUsername: "HighDistortion"} }; function isExist(obj, k, v){ return obj[v]==undefined? false: obj[v][k] == v; } console.log(isExist(object_data, 'eSportsUsername', 'Dark')); console.log(isExist(object_data, 'eSportsUsername', 'No Dark'));

Used recursion to match value in nested object.使用递归匹配嵌套 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参考: Array.prototype.entiresArray.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'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM