admin管理员组

文章数量:1398840

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 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

Share Improve this question asked Dec 14, 2019 at 15:14 bosskay972bosskay972 1,0051 gold badge9 silver badges32 bronze badges 3
  • 5 Array.prototype.filter – ASDFGerte Commented Dec 14, 2019 at 15:15
  • Yep, it's work fine thanks, post it as answer to close this subject – bosskay972 Commented Dec 14, 2019 at 15:17
  • 1 There is already an answer with this. PS: it pays to just read the general description of all array methods at least once: MDN, there are only like 30-40. Especially when "working a lot with arrays". – ASDFGerte Commented Dec 14, 2019 at 15:20
Add a ment  | 

1 Answer 1

Reset to default 10

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

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

本文标签: arraysIs there any builtin function like find all in javascriptStack Overflow