Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to obtain values from Array in JS in an optimal way

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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://javascript.plainenglish.io/are-for-loops-better-than-arrays-filter-or-foreach-methods-f54b6880d201

https://www.measurethat.net/Benchmarks/Show/4261/0/find-vs-forof-vs-for-loop

>Solution :

Assuming the codi values are unique convert the array to a Map or an object using the codi value as the key. It makes it simple to get at the data then.

This old question/set of answers re the value of which structure to use will be very helpful on deciding which to use.

Map:

const arr=[{codi:"291",literal:"MEDIFIATC Progres 2010 ",check:!0},{codi:"292",literal:"MEDIFIATC Progres < 2010 ",check:!0},{codi:"293",literal:"MEDIFIATC Progres ",check:!0},{codi:"294",literal:"MEDIFIATC Progres 2013  ",check:!0},{codi:"295",literal:"MEDIFIATC Progres 2014 ",check:!0},{codi:"296",literal:"MEDIFIATC MEDIFIATC  ",check:!0},{codi:"297",literal:"MEDIFIATC MEDIFIATC P5 ",check:!0},{codi:"298",literal:"MEDIFIATC MEDIFIATC P15 ",check:!0},{codi:"299",literal:"MEDIFIATC MEDIFIATC DIAGONAL ",check:!0}];

const map = new Map();

for (const obj of arr) {
  map.set(obj.codi, obj);
}

console.log(map.get('298'));

Object:

const arr=[{codi:"291",literal:"MEDIFIATC Progres 2010 ",check:!0},{codi:"292",literal:"MEDIFIATC Progres < 2010 ",check:!0},{codi:"293",literal:"MEDIFIATC Progres ",check:!0},{codi:"294",literal:"MEDIFIATC Progres 2013  ",check:!0},{codi:"295",literal:"MEDIFIATC Progres 2014 ",check:!0},{codi:"296",literal:"MEDIFIATC MEDIFIATC  ",check:!0},{codi:"297",literal:"MEDIFIATC MEDIFIATC P5 ",check:!0},{codi:"298",literal:"MEDIFIATC MEDIFIATC P15 ",check:!0},{codi:"299",literal:"MEDIFIATC MEDIFIATC DIAGONAL ",check:!0}];

const out = {};

for (const obj of arr) {
  out[obj.codi] = obj;
}

console.log(out['298']);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading