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

180
Visualizações
Seeing if a value is present in a dictionary

Is there a way to see if an item is located in a dictionary values.

My attempt:

var inventory_needed = [
    { section: "hardware",          supplies: "hammers"                       },
    { section: "plumbing",          supplies: "pipes"                         },
    { section: "garden",            supplies: "grass seeds"                   },
    { section: "cleaning supplies", supplies: ["hand sanitizer", "detergent"] },
    { section: "appliances",        supplies: ["fridges", "dishwashers"]      } 
];

incoming_item = "hand sanitizer";

inventory_needed.forEach(function(item) {
    if(item.supplies.includes(incoming_item)) {
        console.log(incoming_item + 'is on the way');
    }
    else{
        console.log('nothing is on the way');
    }
}

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

0

You could simplify your search this way:

if (inventory_needed.some(item => item.supplies.includes(incoming_item))) {
   console.log(incoming_item + ' is on the way');
} else {
   console.log('nothing is on the way');
}

This will only ever output once. It's not clear whether or not you want the is on the way message to be able to print multiple times.

Array.prototype.some() will let you run a true/false check for each element in the list, and returns whether or not any of them were true.

about 4 years ago · Juan Pablo Isaza Relatório

0

Since your supplies array consist of both string and array, I suggest this below logic for you.

Logic

  • map through the array and generate list of supplies.
  • Array.flat the array to make the array uni directional. I used Array.flatMap to combine both Array.map and Array.flat
  • Generate the unique elements from this array using Set
  • Search for the target element in this Set array using Set.has.

Working Code

const inventory_needed = [
    { section: "hardware", supplies: "hammers" },
    { section: "plumbing", supplies: "pipes" },
    { section: "garden", supplies: "grass seeds" },
    { section: "cleaning supplies", supplies: ["hand sanitizer", "detergent"] },
    { section: "appliances", supplies: ["fridges", "dishwashers"] }
];
const incoming_item = "hand sanitizer";
const uniqueSuppliesList = new Set(inventory_needed.flatMap((item) => item.supplies));
uniqueSuppliesList.has(incoming_item) ? console.log(incoming_item + 'is on the way') : console.log('nothing is on the way');

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