我列出了数组中的id和其他数组中的文章列表.
我想在id的数组中article通过idsfind 过滤我的数组.
例如:
const ids = [ '1', '2', '3' ];
const articles = [
{ id: '1', title: 'blua' },
{ id: '10', title: 'blua' }
...
];
Run Code Online (Sandbox Code Playgroud)
我试过这个:
ids.map((id) => {
return audits.find((audit) => {
return id === audit.id;
});
});
Run Code Online (Sandbox Code Playgroud)
但返回underfined:/
我认为这不是一个好的方法^^
有人可以帮帮我吗?
谢谢 !
使用array.prototype.filter和array.prototype.includes:
const ids = [ '1', '2', '3' ];
const articles = [ { id: '1', title: 'blua' },{ id: '10', title: 'blua' } ];
const filtered = articles.filter(a => ids.includes(a.id));
console.log(filtered);Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
52 次 |
| 最近记录: |