gpt4 book ai didi

javascript - 使用 Lodash 查找集合中的嵌套对象

转载 作者:行者123 更新时间:2023-11-28 14:15:32 24 4
gpt4 key购买 nike

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com