Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

181
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda