bosskay972
bosskay972

Reputation: 993

Is there any built-in function like find all in javascript?

I work a lot with array in javascript, and I'm stuck with the function .find() because it just return the first occurence, I want an array with all occurence if there is many.

Here's my code:

const condition = [
    {
      info_perso: 'Alignement',
      symbole: '=',
      info_perso_value: 'Mercenaire'
    },
    {
      info_perso: 'Alignement',
      symbole: '=',
      info_perso_value: 'Bonta'
    },
    { info_perso: 'Grade', symbole: '>', info_perso_value: '2' }
  ]

console.log(condition.find(el => el.info_perso == 'Alignement'));

I want an array with the first AND the second element. Is there any built-in function like .find() I can use to solve my problem ? If there is no built-in function, is it possible to you to give me some hint to build a function that allow me to do that (this function will be call .findAll()) ?

PS: I use node.js

Upvotes: 2

Views: 3382

Answers (1)

Nick
Nick

Reputation: 16586

Yes, you're looking for Array.prototype.filter

console.log(condition.filter(el => el.info_perso == 'Alignement'));

Upvotes: 10

Related Questions