Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
find lowest value in nested array in javascript

i am trying to find the name with the lowest number after it, but iterating over it is slow with larger lists. For exaple the expected output should be ["bob", 1] ["jeff", 2] ["wal-E",2]

let items = [["bob",1],["jeff",2],["wal-E",2],["bob",1],["bob",10]]
let items2 = []
let Lowest = 0;
for (y in items){
    for (z in items)
      {
        if (items[y][0] == items[z][0] && items[y][1]<=items[z][1]){
          continue;
        } else if (items[y][0] == items[z][0]){
            Lowest += 1
            if (Lowest >= 2){
            break;
            }
            else {
            continue;
            }
        }
        }      
  if (Lowest == 0){
    items2.push(items[y])
  }
}
console.log(items2);

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You could reduce the array and collect the smallest values.

const
    items = [["bob", 1], ["jeff", 2], ["wal-E", 2], ["bob", 1], ["bob", 10]],
    result = Object.entries(items.reduce((r, [name, value]) => {
        if (!(name in r) || r[name] > value) r[name] = value;
        return r;
    }, {}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Relatório

0

Using reduce is an elegant solution to acheive this

let items = [["bob",1],["jeff",2],["wal-E",2],["bob",1],["bob",10]];

const lowestItem = items.reduce((a, b) => a[1] < b[1] ? a : b);

console.log(lowestItem);

Read more about Array.prototype.reduce here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

about 4 years ago · Juan Pablo Isaza Relatório

0

You can store the name with the lowest number and the lowest number in two seperate variables, then loop through the array and check whether each array's second item is smaller than the lowest number.

If so, set the lowest number to the second item in the array and the lowest name to the first item in the array.

let items = [["bob",1],["jeff",2],["wal-E",2],["bob",1],["bob",10]]
let lowest;
let lowestName;

items.forEach(e => {
  if(!lowest || e[1] < lowest){
    lowest = e[1];
    lowestName = e[0];
  }
})

console.log(lowestName, lowest)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda