链接数组方法(filter、map、reduce)而不是使用双循环

我遇到了一个我无法解决的问题......所以这就是我想要做的。


给定以下对象数组,


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 },

];

我知道我可以通过嵌套循环运行它以按成分名称添加键,然后在我循环遍历计数时增加值,如下所示:


      let ingredientCount = {}; 


  for (i = 0; i < products.length; i += 1) {

    for (j = 0; j < products[i].ingredients.length; j += 1) { //loop ingredients 

      ingredientCount[products[i].ingredients[j]] = (ingredientCount[products[i].ingredients[j]] || 0) + 1; 

    }

  }

因此, ingredientCount 应该是这样的:{ "artichoke": 1 "mushrooms": 2 } ***


这里的问题是我需要使用 map 和 reduce 来创建与上面相同的结果。


let ingredientCount = {}


    ingredientCount = 

    products.filter ((value) => {

        // filter out arrays within ingredients 

        // so out come should be like  

        /* 

        [ingredients: ['artichoke', 'sundried tomatoes', 'mushrooms']

        ,ingredients: ['roma', 'sundried tomatoes', 'goats cheese', 'rosemary']

        ,ingredients: ['black beans', 'jalapenos', 'mushrooms']

        ,ingredients: ['blue cheese', 'garlic', 'walnuts']

        ,ingredients: ['spinach', 'kalamata olives', 'sesame seeds']

        */


    }).map ((value) => {

        /* then take out ingredients and map this array to  

        arthichoke: ['artichoke','artichoke','artichoke']

        sundried tomatoes: ['sundried tomatoes']

        etc... 

        */


    

    })


无论如何,我可以使用上面的数组方法来获得完全相同的结果而不必使用循环吗?


提前致谢。


温温酱
浏览 185回答 5
5回答

慕妹3146593

使用flatMap和reduce。(除了??和,运算符)const products = [&nbsp; &nbsp; { name: 'Sonoma', ingredients: ['artichoke', 'sundried tomatoes', 'mushrooms'], containsNuts: false },&nbsp; &nbsp; { name: 'Pizza Primavera', ingredients: ['roma', 'sundried tomatoes', 'goats cheese', 'rosemary'], containsNuts: false },&nbsp; &nbsp; { name: 'South Of The Border', ingredients: ['black beans', 'jalapenos', 'mushrooms'], containsNuts: false },&nbsp; &nbsp; { name: 'Blue Moon', ingredients: ['blue cheese', 'garlic', 'walnuts'], containsNuts: true },&nbsp; &nbsp; { name: 'Taste Of Athens', ingredients: ['spinach', 'kalamata olives', 'sesame seeds'], containsNuts: true },];const ingredientCount = products&nbsp; .flatMap(({ ingredients }) => ingredients)&nbsp; .reduce((acc, item) => ((acc[item] = (acc[item] ?? 0) + 1), acc), {});console.log(ingredientCount);

ibeautiful

您需要使用map(),flat()然后使用reduce()。该函数flat()展平数组。products = [&nbsp; &nbsp; { name: 'Sonoma', ingredients: ['artichoke', 'sundried tomatoes', 'mushrooms'], containsNuts: false },&nbsp; &nbsp; { name: 'Pizza Primavera', ingredients: ['roma', 'sundried tomatoes', 'goats cheese', 'rosemary'], containsNuts: false },&nbsp; &nbsp; { name: 'South Of The Border', ingredients: ['black beans', 'jalapenos', 'mushrooms'], containsNuts: false },&nbsp; &nbsp; { name: 'Blue Moon', ingredients: ['blue cheese', 'garlic', 'walnuts'], containsNuts: true },&nbsp; &nbsp; { name: 'Taste Of Athens', ingredients: ['spinach', 'kalamata olives', 'sesame seeds'], containsNuts: true },];let obj = products&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(p => p.ingredients)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .flat()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .reduce((obj, val) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj[val] = (obj[val] || 0) + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return obj;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, {});console.log(obj);

当年话下

使用数组的 .reduce 方法和 .forEach 方法的解决方案。var products = [&nbsp; &nbsp; { name: 'Sonoma', ingredients: ['artichoke', 'sundried tomatoes', 'mushrooms'], containsNuts: false },&nbsp; &nbsp; { name: 'Pizza Primavera', ingredients: ['roma', 'sundried tomatoes', 'goats cheese', 'rosemary'], containsNuts: false },&nbsp; &nbsp; { name: 'South Of The Border', ingredients: ['black beans', 'jalapenos', 'mushrooms'], containsNuts: false },&nbsp; &nbsp; { name: 'Blue Moon', ingredients: ['blue cheese', 'garlic', 'walnuts'], containsNuts: true },&nbsp; &nbsp; { name: 'Taste Of Athens', ingredients: ['spinach', 'kalamata olives', 'sesame seeds'], containsNuts: true },];var result = products.reduce((acc,obj) =>&nbsp;&nbsp; &nbsp;{obj.ingredients.forEach(ob=> acc[ob] = acc[ob]+1 || 1)&nbsp; &nbsp; return acc;},{});console.log(result);

qq_遁去的一_1

用于Array.prototype.reduce减少数组并增加计数。const products = [{&nbsp; &nbsp; name: 'Sonoma',&nbsp; &nbsp; ingredients: ['artichoke', 'sundried tomatoes', 'mushrooms'],&nbsp; &nbsp; containsNuts: false&nbsp; },&nbsp; {&nbsp; &nbsp; name: 'Pizza Primavera',&nbsp; &nbsp; ingredients: ['roma', 'sundried tomatoes', 'goats cheese', 'rosemary'],&nbsp; &nbsp; containsNuts: false&nbsp; },&nbsp; {&nbsp; &nbsp; name: 'South Of The Border',&nbsp; &nbsp; ingredients: ['black beans', 'jalapenos', 'mushrooms'],&nbsp; &nbsp; containsNuts: false&nbsp; },&nbsp; {&nbsp; &nbsp; name: 'Blue Moon',&nbsp; &nbsp; ingredients: ['blue cheese', 'garlic', 'walnuts'],&nbsp; &nbsp; containsNuts: true&nbsp; },&nbsp; {&nbsp; &nbsp; name: 'Taste Of Athens',&nbsp; &nbsp; ingredients: ['spinach', 'kalamata olives', 'sesame seeds'],&nbsp; &nbsp; containsNuts: true&nbsp; },];const result = products.reduce((acc, { ingredients }) => {&nbsp; ingredients.forEach((ingredient) => {&nbsp; &nbsp; acc[ingredient] = (acc[ingredient] || 0) + 1;&nbsp; });&nbsp; return acc;}, Object.create(null));console.log(result);

慕的地10843

您可以使用两个forEachor map,并维护一个只需要更新的最终数组。let products = [&nbsp; &nbsp; { name: 'Sonoma', ingredients: ['artichoke', 'sundried tomatoes', 'mushrooms'], containsNuts: false },&nbsp; &nbsp; { name: 'Pizza Primavera', ingredients: ['roma', 'sundried tomatoes', 'goats cheese', 'rosemary'], containsNuts: false },&nbsp; &nbsp; { name: 'South Of The Border', ingredients: ['black beans', 'jalapenos', 'mushrooms'], containsNuts: false },&nbsp; &nbsp; { name: 'Blue Moon', ingredients: ['blue cheese', 'garlic', 'walnuts'], containsNuts: true },&nbsp; &nbsp; { name: 'Taste Of Athens', ingredients: ['spinach', 'kalamata olives', 'sesame seeds'], containsNuts: true },];let iCount = {};products.forEach((c) => c.ingredients.forEach((i) => iCount.hasOwnProperty(i) ? iCount[i]++ : iCount[i] = 1));console.log(iCount);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript