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

I work a lot with array in javascript, and I'm stuck with the function .find() because it just ret

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'));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744122863a4559481.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信