我有以下
const A = ['test 105', 'test 300']
const B = [
{ name: 'test 105', id: 1 },
{ name: 'test 300', id: 2 },
{ name: 'test 3', id: 29 },
{ name: 'test 20', id: 20 }
]
Run Code Online (Sandbox Code Playgroud)
我需要检查 const B.name 是否等于 const A 中的任何值,并仅返回来自 const B 的匹配项。
所以我需要最后的回报
const B = [
{ name: 'test 105', id: 1 },
{ name: 'test 300', id: 2 }
]
Run Code Online (Sandbox Code Playgroud)
我能够用 1 个元素来做到这一点,但我想检查 const A,因为它将来可能有更多的值
const filtered = parsedResponse.filter(element => element.name == 'test 105')
console.log(filtered)
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏谢谢
您可以根据第二个数组中对象的存在将其过滤掉。
const A = ['test 105', 'test 300'];
const B = [ { name: 'test 105', id: 1 }, { name: 'test 300', id: 2 }, { name: 'test 3', id: 29 }, { name: 'test 20', id: 20 } ];
const result = B.filter(k=>A.includes(k.name));
console.log(result);Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40 次 |
| 最近记录: |