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
1 Answer
Reset to default 10Yes, 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条)