0

I've list of id's in array and list of article in other array.

I would like filter my article array by ids find in id's array.

Exemple :

const ids = [ '1', '2', '3' ];
const articles = [
  { id: '1', title: 'blua' },
  { id: '10', title: 'blua' }
  ...
];

I've try this :

ids.map((id) => {
  return audits.find((audit) => {
    return id === audit.id;
  });
});

But return underfined :/

I think it's not a good methode ^^

Anyone can help me ?

Thank you !

1
  • Cause theres no audits array?! Commented Jan 15, 2018 at 14:52

2 Answers 2

6

Use array.prototype.filter and 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);

Sign up to request clarification or add additional context in comments.

1 Comment

Maybe even better using a Set: ids = new Set(ids); filtered = articles.filter(({id}) => ids.has(id))
0
const ids = [ '1', '2', '3' ];
const articles = [
  { id: '1', title: 'blua' },
  { id: '10', title: 'blua' }
  ...
];

let results = articles.filter( (a) => ids.indexOf(a.id) !== -1);

1 Comment

adding code only to answers make them pop up as a spam or low-quality post, although you have provided the solution try adding some description to what is happening.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.