简体   繁体   English

使用Lodash在集合中查找嵌套对象

[英]Finding nested object in a collection with Lodash

var collection = {
  'key1': [
    { 'uuid': '123', 'a': 'a' },
    { 'uuid': '456', 'b': 'b',
      'randomKeyValue': [
        { 'uuid': '349', 'd': 'd' }
      ]
    }
  ],
  'key2': [
    { 'uuid': '890', 'c': 'c' }
  ]
}

Using _.find(collection, { 'uuid': '349' }) will return undefined. 使用_.find(collection, { 'uuid': '349' })将返回未定义。

How to find the hash that has uuid == 349 ? 如何查找具有uuid == 349的哈希?

The expected return of the find is: { 'uuid': '349', 'd': 'd' } 该查找的预期返回是: { 'uuid': '349', 'd': 'd' }

Must be written with ES5 standards and must use Lodash. 必须使用ES5标准编写,并且必须使用Lodash。

It's fairly straightforward using plain js 使用普通js相当简单

 var collection = { 'key1': [ { 'uuid': '123', 'a': 'a' }, { 'uuid': '456', 'b': 'b' } ], 'key2': [ { 'uuid': '890', 'c': 'c' }, { 'uuid': '349', 'd': 'd' } ] } const res = Object.values(collection).flat().find(({uuid}) => uuid === '349'); console.log(res); 

_.filter(_.flatMap(collection), function(o) { return o.uuid == '349' });

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

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