1

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');
    }
}
Phil
  • 157,677
  • 23
  • 242
  • 245
bombombs
  • 593
  • 1
  • 13
  • 1
    You're missing a `)` on the last line... should be `})`. Just a typo here in your question or is your actual code like that too? – Phil Jan 10 '22 at 05:21
  • `const result = inventory_needed.filter(o => o.supplies.includes(incoming_item))` you can test if the length of result is > 0, and see which items match. – danh Jan 10 '22 at 05:22
  • 2
    Be careful with that data structure. I would suggest that all `supplies` should always be an array, even with only a single item. Your current code will return false positives for something like `supplies: "hand sanitizer for snakes"` – Phil Jan 10 '22 at 05:25
  • With your design, you should check if `supplies` is array first. – Ricky Mo Jan 10 '22 at 05:25
  • https://jsfiddle.net/53arLzm6/ see if this code works for you – Viswanath Polaki Jan 10 '22 at 06:20

2 Answers2

1

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.

Nathan Wiles
  • 841
  • 10
  • 30
1

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');
Nitheesh
  • 19,238
  • 3
  • 22
  • 49
  • 1
    Why convert the `Set` back to an array? You can just use `set.has(incoming_item)` which is actually a quicker lookup – Phil Jan 10 '22 at 06:07
  • Also, how on Earth did you add an answer 11 mins after the question was closed? – Phil Jan 10 '22 at 06:07