var collection = {
'key1': [
{ 'uuid': '123', 'a': 'a' },
{ 'uuid': '456', 'b': 'b',
'randomKeyValue': [
{ 'uuid': '349', 'd': 'd' }
]
}
],
'key2': [
{ 'uuid': '890', 'c': 'c' }
]
}
使用 _.find(collection, { 'uuid': '349' })
将返回 undefined。
如何查找具有uuid == 349
的哈希值?
查找的预期返回为:{ 'uuid': '349', 'd': 'd' }
必须使用 ES5 标准编写,并且必须使用 Lodash。
最佳答案
使用纯 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);
关于javascript - 使用 Lodash 查找集合中的嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57669969/