在数组ID中查找对象

s-l*_*ndz 0 javascript

我列出了数组中的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:/

我认为这不是一个好的方法^^

有人可以帮帮我吗?

谢谢 !

Fal*_*aly 6

使用array.prototype.filterarray.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)