简体   繁体   中英

Extract specific values from multidimensional array javascript

I have a multidimensional array seen below. I wish extract all the ingredients values from it, and then do an action with them, like count how many of each there are.

var products = [
{ name: "Sonoma", ingredients: ["artichoke", "sundried tomatoes", "mushrooms"], containsNuts: false },
{ name: "Pizza Primavera", ingredients: ["roma", "sundried tomatoes", "goats cheese", "rosemary"], containsNuts: false },
{ name: "South Of The Border", ingredients: ["black beans", "jalapenos", "mushrooms"], containsNuts: false },
{ name: "Blue Moon", ingredients: ["blue cheese", "garlic", "walnuts"], containsNuts: true },
{ name: "Taste Of Athens", ingredients: ["spinach", "kalamata olives", "sesame seeds"], containsNuts: true }
];

I have done this using a function from Underscore.js, _.flatten() , and the vanilla javascript function map() , but I had to use map() twice. Once for extracting all the ingredients, and then to make a hash style structure that counts up the number of times each ingredient occurs. I store these counts in a separate object, ingredientCount in this case.

var ingredientCount = {};
_.flatten(products.map(function(x) {
    return x.ingredients;
})).map(function(y){
    return ingredientCount[y] = (ingredientCount[y] || 0) + 1;
});

console.log(ingredientCount); will output the following list:

{ artichoke: 1,
  'sundried tomatoes': 2,
  mushrooms: 2,
  roma: 1,
  'goats cheese': 1,
  rosemary: 1,
  'black beans': 1,
  jalapenos: 1,
  'blue cheese': 1,
  garlic: 1,
  walnuts: 1,
  spinach: 1,
  'kalamata olives': 1,
  'sesame seeds': 1 }

I have solved my problem, but I feel like there should be a cleaner, more efficient way to do this without using map() twice. Can anyone help me out? I apologize in advance if this is a duplicate question.

This is the most elegant way I can think of in plain JavaScript, as it doesn't use any intermediary data structures (the original solution creates two arrays which are then combined into the final object).

var counts = products.reduce(function (result, product) {
    product.ingredients.forEach(function (ingredient) {
        result[ingredient] = (result[ingredient] || 0) + 1;
    });
    return result;
}, {});

You could use recursion:

 var products = [ { name: "Sonoma", ingredients: ["artichoke", "sundried tomatoes", "mushrooms"], containsNuts: false }, { name: "Pizza Primavera", ingredients: ["roma", "sundried tomatoes", "goats cheese", "rosemary"], containsNuts: false }, { name: "South Of The Border", ingredients: ["black beans", "jalapenos", "mushrooms"], containsNuts: false }, { name: "Blue Moon", ingredients: ["blue cheese", "garlic", "walnuts"], containsNuts: true }, { name: "Taste Of Athens", ingredients: ["spinach", "kalamata olives", "sesame seeds"], containsNuts: true } ]; // written by hand, with <3 function ic(p,o,i,b,g) { i|=0;g|=0; if(!b&&p&&!p.ingredients) while((g=ic(p[g].ingredients,o,0,g+1))&&p[g]); else { o[p[i]]=(o[p[i]]|0)+1; return p[i+1]?ic(p,o,i+1,b):b; } } var ingredientCount = {}; ic(products, ingredientCount); document.body.innerHTML = JSON.stringify(ingredientCount).replace(/(\\,)/g,"$1<br/>"); 
 body {font-family: monospace } 
 <body></body> 

EDIT: No i'm not serious. But its almost 3x faster

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM