Hello! I am a beginner, I need your help, there is such code:

var data =[ { "id": "0001", "type": "donut", "name": "Cake", "ppu": 0.55, "batters": { "batter": [ { "id": "1001", "type": "Regular" }, { "id": "1002", "type": "Chocolate" }, { "id": "1003", "type": "Blueberry" }, { "id": "1004", "type": "Devil's Food" } ] }, "topping": [ { "id": "5001", "type": "None" }, { "id": "5002", "type": "Glazed" }, { "id": "5005", "type": "Sugar" }, { "id": "5007", "type": "Powdered Sugar" }, { "id": "5006", "type": "Chocolate with Sprinkles" }, { "id": "5003", "type": "Chocolate" }, { "id": "5004", "type": "Maple" } ] }, { "id": "0002", "type": "donut", "name": "Raised", "ppu": 0.55, "batters": { "batter": [ { "id": "1001", "type": "Regular" } ] }, "topping": [ { "id": "5001", "type": "None" }, { "id": "5002", "type": "Glazed" }, { "id": "5005", "type": "Sugar" }, { "id": "5003", "type": "Chocolate" }, { "id": "5004", "type": "Maple" } ] }, { "id": "0003", "type": "donut", "name": "Old Fashioned", "ppu": 0.55, "batters": { "batter": [ { "id": "1001", "type": "Regular" }, { "id": "1002", "type": "Chocolate" } ] }, "topping": [ { "id": "5001", "type": "None" }, { "id": "5002", "type": "Glazed" }, { "id": "5003", "type": "Chocolate" }, { "id": "5004", "type": "Maple" } ] } ] var donutSearch = "Chocolate"; var donutId = data.filter(function(val) { return val.id === donutSearch; })[0].id; 

I need to make sure that all values ​​with "type": "Chocolate" are displayed in the console. My code above gives the error "Cannot read property 'id' of undefined" I also tried this:

 let searchTerm = "Chocolate"; let battersId = data.find(donut => donut.type === searchTerm).id 

Same error as above

Can you please tell me any solutions

  • After filtering you need a value not found, that is, an empty array. And you are trying to contact him by [0] - Sergey Konovalov
  • You have "type": "Chocolate" in several places. What do you need? - Stepan Kasyanenko 2:01 pm
  • @StepanKasyanenko at least before the first - Alexandr
  • "type": "Chocolate" - at the third nesting level, and only the first one, which has only donut type, is checked, so nothing is found - Grundy
  • @Grundy case says. I wanted to say the same thing, but it didn't work out) - Stepan Kasyanenko

1 answer 1

This is how it is possible if the structure is always the same as you wrote:

 let search = "Chocolate"; data.forEach(d => { filterAndLog(d.batters.batter); filterAndLog(d.topping); }); function filterAndLog(arr){ arr.filter(b => b.type === search).forEach(c => console.log(c)) } 

 var data =[ { "id": "0001", "type": "donut", "name": "Cake", "ppu": 0.55, "batters": { "batter": [ { "id": "1001", "type": "Regular" }, { "id": "1002", "type": "Chocolate" }, { "id": "1003", "type": "Blueberry" }, { "id": "1004", "type": "Devil's Food" } ] }, "topping": [ { "id": "5001", "type": "None" }, { "id": "5002", "type": "Glazed" }, { "id": "5005", "type": "Sugar" }, { "id": "5007", "type": "Powdered Sugar" }, { "id": "5006", "type": "Chocolate with Sprinkles" }, { "id": "5003", "type": "Chocolate" }, { "id": "5004", "type": "Maple" } ] }, { "id": "0002", "type": "donut", "name": "Raised", "ppu": 0.55, "batters": { "batter": [ { "id": "1001", "type": "Regular" } ] }, "topping": [ { "id": "5001", "type": "None" }, { "id": "5002", "type": "Glazed" }, { "id": "5005", "type": "Sugar" }, { "id": "5003", "type": "Chocolate" }, { "id": "5004", "type": "Maple" } ] }, { "id": "0003", "type": "donut", "name": "Old Fashioned", "ppu": 0.55, "batters": { "batter": [ { "id": "1001", "type": "Regular" }, { "id": "1002", "type": "Chocolate" } ] }, "topping": [ { "id": "5001", "type": "None" }, { "id": "5002", "type": "Glazed" }, { "id": "5003", "type": "Chocolate" }, { "id": "5004", "type": "Maple" } ] } ] let search = "Chocolate"; data.forEach(d => { filterAndLog(d.batters.batter); filterAndLog(d.topping); }); function filterAndLog(arr){ arr.filter(b => b.type === search).forEach(c => console.log(c)) } 

Here is an option with deep search, without calculating the circular links ..

 let search = "Chocolate"; traverse(data) function traverse(obj) { Object.keys(obj).map(key => { let value = obj[key]; typeof value === "object" && traverse(value); key === "type" && value === search && console.log(obj); }); } 

 var data =[ { "id": "0001", "type": "donut", "name": "Cake", "ppu": 0.55, "batters": { "batter": [ { "id": "1001", "type": "Regular" }, { "id": "1002", "type": "Chocolate" }, { "id": "1003", "type": "Blueberry" }, { "id": "1004", "type": "Devil's Food" } ] }, "topping": [ { "id": "5001", "type": "None" }, { "id": "5002", "type": "Glazed" }, { "id": "5005", "type": "Sugar" }, { "id": "5007", "type": "Powdered Sugar" }, { "id": "5006", "type": "Chocolate with Sprinkles" }, { "id": "5003", "type": "Chocolate" }, { "id": "5004", "type": "Maple" } ] }, { "id": "0002", "type": "donut", "name": "Raised", "ppu": 0.55, "batters": { "batter": [ { "id": "1001", "type": "Regular" } ] }, "topping": [ { "id": "5001", "type": "None" }, { "id": "5002", "type": "Glazed" }, { "id": "5005", "type": "Sugar" }, { "id": "5003", "type": "Chocolate" }, { "id": "5004", "type": "Maple" } ] }, { "id": "0003", "type": "donut", "name": "Old Fashioned", "ppu": 0.55, "batters": { "batter": [ { "id": "1001", "type": "Regular" }, { "id": "1002", "type": "Chocolate" } ] }, "topping": [ { "id": "5001", "type": "None" }, { "id": "5002", "type": "Glazed" }, { "id": "5003", "type": "Chocolate" }, { "id": "5004", "type": "Maple" } ] } ] let search = "Chocolate"; traverse(data) function traverse(obj) { Object.keys(obj).map(key => { let value = obj[key]; typeof value === "object" && traverse(value); key === "type" && value === search && console.log(obj); }); } 

  • more forEach god forEach :) - Grundy
  • @Grundy Well, yes, I could write another map =) - Stranger in the Q
  • @Stranger in the Q and what does filterandLog mean? - Alexandr
  • @Alexandr uh, I didn’t understand the question, this is the function I created .. - Stranger in the Q
  • @Stranger in the Q into which the properties are transferred, I understand correctly? - Alexandr