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)
添加回答
舉報