[英]Chaining array methods (filter, map, reduce) instead of using double loop
我遇到了一個我無法解決的問題......所以這就是我想要做的。
給定以下對象數組,
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;
}
}
因此,成分計數應該是這樣的:{“朝鮮薊”:1“蘑菇”: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... */ }).reduce((acc, value) => { /* then reduce arrays within each key to numbers. hence, the output should be artichokes: artichokes.length (ie 3 ) sundried toamatoes: 1 */ })
無論如何,我可以使用上述數組方法獲得完全相同的結果而不必使用循環嗎?
提前致謝。
您需要使用map()
、 flat()
和reduce()
。 function flat()
使陣列變平。
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 obj = products.map(p => p.ingredients).flat().reduce((obj, val) => { obj[val] = (obj[val] || 0) + 1; return obj; }, {}); console.log(obj);
您可以使用兩個forEach
或map
,並維護一個只需要更新的最終數組。
let 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 iCount = {}; products.forEach((c) => c.ingredients.forEach((i) => iCount.hasOwnProperty(i)? iCount[i]++: iCount[i] = 1)); console.log(iCount);
使用數組的.reduce 方法和.forEach 方法的解決方案。
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 }, ]; var result = products.reduce((acc,obj) => {obj.ingredients.forEach(ob=> acc[ob] = acc[ob]+1 || 1) return acc; },{}); console.log(result);
使用Array.prototype.reduce
減少數組並在 go 時增加計數。
const 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 }, ]; const result = products.reduce((acc, { ingredients }) => { ingredients.forEach((ingredient) => { acc[ingredient] = (acc[ingredient] || 0) + 1; }); return acc; }, Object.create(null)); console.log(result);
使用flatMap
和reduce
。 (除了??
和,
運算符)
const 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 }, ]; const ingredientCount = products.flatMap(({ ingredients }) => ingredients).reduce((acc, item) => ((acc[item] = (acc[item]?? 0) + 1), acc), {}); console.log(ingredientCount);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.