Get object inside an array that has the lowest value in a specific Javascript key

Asked

Viewed 138 times

2

I got the following array object:

const cores = [
  { cor: 'amarelo', peso: 2 },
  { cor: 'amarelo', peso: 3 },
  { cor: 'verde', peso: 7 }
];

My goal is first to filter it to return only the objects with the yellow color, and this I do with:

const mani = obj.filter(spec => spec.cor === 'amarelo');

After that, I have to filter this array to return only the object with the least weight. In this case, I hope to receive the following object:

{ cor: 'amarelo', peso: 2 }

How would I do that?

2 answers

4


The simplest way is to use a loop for for this. In this way, we will create a variable to store the object that satisfies our needs. At each iteration we will check whether the object of the current iteration is smaller than the object of bigger. If it is, we’ll trade. Something like that:

const cores = [
  { cor: 'amarelo', peso: 2 },
  { cor: 'amarelo', peso: 3 },
  { cor: 'verde', peso: 7 }
];

const filtered = cores.filter((spec) => spec.cor === 'amarelo');

let bigger; // Criamos uma variável para armazenar o objeto com menor ID.

for (const obj of filtered) {
  // Se `obj` for `null` ou tiver um peso maior que o da iteração
  // atual, iremos trocar pelo objeto atual.
  if (!bigger || obj.peso < bigger.peso) {
    bigger = obj;
  }
}

console.log(bigger);

If you’re really concerned about performance, the filter no longer necessary, since you can create a slightly more complex conditional on if within the for for that. So:

if ((!bigger || obj.peso < bigger.peso) && obj.cor === 'amarelo') {
  /* ... */
}

Basically, it checks if the object has cor equal to amarelo and if no object has been defined or if the weight of the object of the current iteration is less than the one already saved in bigger.

const cores = [
  { cor: 'não-amarelo', peso: 0 }, // O menor peso, mas não é amarelo.
  { cor: 'amarelo', peso: 2 },
  { cor: 'amarelo', peso: 3 },
  { cor: 'verde', peso: 7 }
];

let bigger; // Criamos uma variável para armazenar o objeto com menor ID.

for (const obj of cores) {
  if ((!bigger || obj.peso < bigger.peso) && obj.cor === 'amarelo') {
    bigger = obj;
  }
}

console.log(bigger);


You can even opt for more declarative approaches using the reduce, since the for is a somewhat more imperative and "less pure" approach, since eventually we are mutating bigger.

const cores = [
  { cor: 'não-amarelo', peso: 0 },
  { cor: 'amarelo', peso: 2 },
  { cor: 'amarelo', peso: 3 },
  { cor: 'verde', peso: 7 }
];

const bigger = cores.reduce((saved, current) => {
  return ((!saved || current.peso < saved.peso) && current.cor === 'amarelo') ? current : saved;
}, null);

console.log(bigger);

Reference

1

Just sort your object by weight and bring the first position of your index:

  const mani = cores.filter(spec => (spec.cor === 'amarelo')).sort((a, b) => {
  if(a.peso < b.peso) {
    return -1;
   }
})[0];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.