我想更好地学习JS看到次项目的数量增加值。使用underscore.js基于阵列
我有下面的代码,我试图投入underscore.js但我失败了。
我希望你能指出我要去哪里错了。
我试图采取一个循环,我知道的作品,然后用下划线capabilites来完善它。最上面的测试显示了循环,第二个测试是我试图用underscore.js做同样的事情。我失败了!
感谢
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 }
];
it("should count the ingredient occurrence (imperative)", function() {
var ingredientCount = { "{ingredient name}": 0 };
for (i = 0; i < products.length; i+=1) {
for (j = 0; j < products[i].ingredients.length; j+=1) {
ingredientCount[products[i].ingredients[j]] = (ingredientCount[products[i].ingredients[j]] || 0) + 1;
}
}
expect(ingredientCount['mushrooms']).toBe(2);
});
it("should count the ingredient occurrence (functional)", function() {
var ingredientCount = { "{ingredient name}": 0 };
var ffd = _(products).chain()
.map(function(x){return x.ingredients;})
.flatten()
.reduce(function(memo,x){
if (x===memo)
{
return ingredientCount[memo] = ingredientCount[memo]+1;
}
else
{
return ingredientCount[memo] = 0;
}
})
.value();
/* chain() together map(), flatten() and reduce() */
expect(ingredientCount['mushrooms']).toBe(2);
});
所以嗯...什么你的问题是什么呢? – asawyer
@asawyer更新后,对不起 – Jon
这一刻我没有时间讨论这个问题,但作为一个附注,'map(function(x){return x.ingredients;})'与'pluck('成分') – asawyer