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
1 Answer
Reset to default 10Yes, 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
版权声明:本文标题:arrays - Is there any built-in function like find all in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744122862a2591820.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论