Home > OS >  How to obtain values from Array in JS in an optimal way
How to obtain values from Array in JS in an optimal way

Time:07-25

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.

CodePudding user response:

By "optimal" if you mean the most performant you should do benchmarks, and even then there can be differences in different browsers and run-time environments.

You can use more recent array methods in JS like find, keep in mind that filter will be definitively slower because it keeps iterating even when it finds the first match because searching for more matches.

item = listaSubgrupo.find(i => i.codi === codi)

CodePudding user response:

Optimal would be to invest in changing the object to a map from the codi to the actual value. Afterwards any other extraction would be O(1).

var obj = {
  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}
}

var result = Object.values(obj).reduce(function(agg,item) {
  agg[item.codi] = item;
  return agg;
}, {})

console.log(result);
// now you can:
console.log(result["294"]);

CodePudding user response:

did you try to use array.filter or array.find, see more here > https://www.w3schools.com/jsref/jsref_find.asp and > https://www.w3schools.com/jsref/jsref_filter.asp you can write it inside your array.map function by using if function to filter them.

CodePudding user response:

there are so many methods like

array.find()

array.filter()

and if you want to perform a operation on every value in the array then use:

array.map()

array.forEach()

these can said to be optimal methods to interact with array

  • Related