2 回答
TA贡献1827条经验 获得超9个赞
而不是使用Array.find,您需要使用Array.filter来获得匹配的结果。
const deps = {
"something": [
{
"type": "static",
"source": "foo",
"target": "bar"
},
{
"type": "static",
"source": "return-me",
"target": "find-me"
}
],
"anything": [
{
"type": "static",
"source": "and-me",
"target": "find-me"
}
],
"no-match": [
{
"type": "static",
"source": "foo",
"target": "bar"
}
]
};
const result = Object.values(deps)
.flat()
.filter(({ target }) => target === 'find-me')
.map(({ source }) => source);
console.log(result);
TA贡献1820条经验 获得超2个赞
替换Array.find()为Array.filter()可以返回多个结果:
const deps = {"something":[{"type":"static","source":"foo","target":"bar"},{"type":"static","source":"return-me","target":"find-me"}],"anything":[{"type":"static","source":"and-me","target":"find-me"}],"no-match":[{"type":"static","source":"foo","target":"bar"}]}
const search = 'find-me'
const sources = Object
.values(deps)
.flat()
.filter(el => el.target === search)
.map(el => el.source)
console.log(sources)
添加回答
举报