I need to obtain a few values from a MAP in JS based on its id ("codi" in the object).
My array is something like this but bigger:
0: {codi: '291', literal: 'MEDIFIATC Progres 2010 ', check: true}
1: {codi: '292', literal: 'MEDIFIATC Progres < 2010 ', check: true}
2: {codi: '293', literal: 'MEDIFIATC Progres ', check: true}
3: {codi: '294', literal: 'MEDIFIATC Progres 2013 ', check: true}
4: {codi: '295', literal: 'MEDIFIATC Progres 2014 ', check: true}
5: {codi: '296', literal: 'MEDIFIATC MEDIFIATC ', check: true}
6: {codi: '297', literal: 'MEDIFIATC MEDIFIATC P5 ', check: true}
7: {codi: '298', literal: 'MEDIFIATC MEDIFIATC P15 ', check: true}
8: {codi: '299', literal: 'MEDIFIATC MEDIFIATC DIAGONAL ', check: true}
Currently I am working on a simple loop, I iterate an when my variable is equals to the "codi" and return the entry.
function obtenerSubgrupo(codi) {
for(j = 0; j < Object.keys(listaSubgrupo).length; j++) {
if (codi == listaSubgrupo[i].codi) {
return listaSubgrupo[i];
}
}
}
How can I improve this?
I also need to extract the values that i am going to return from my main array, currently working with the info of: How can I remove a specific item from an array? but any help on that will be welcome too
Thanks.
EDIT: Just to put here the links shared by Simone Rossaini in the comments as i think they are quite useful:
https://www.measurethat.net/Benchmarks/Show/4261/0/find-vs-forof-vs-for-loop