1

我正在尝试更好地学习 JS。

我有以下代码,我试图将其放入 underscore.js 但失败了。

我希望你能指出我哪里出错了。

我正在尝试采用我知道有效的循环,然后使用下划线功能对其进行改进。上面的测试显示了循环,第二个测试是我尝试对 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);
  });
4

2 回答 2

3

你的 reduce 不是很实用。看看它的文档!“备忘录”,也称为“累加器”,是上一次迭代返回的值——应该是你的ingredientCount地图。

var ingredientCount = _.chain(products)
  .pluck("ingredients") // don't use map for that
  .flatten()
  .reduce(function (memo, item) {
       memo[item] = (memo[item] || 0)+1;
       /* alternative:
       if (item in memo)
           memo[item]++;
       else
           memo[item] = 1;
       */
       return memo; // to be used in the next iteration!
  }, {/*"{ingredient name}": 0*/})
  .value();

请注意memo === ingredientCount

于 2013-01-15T17:06:58.970 回答
0

我会使用更简单的代码来理解代码,尽管它不起作用:

it("should count the ingredient occurrence (functional)", function () {

  var ingredientCount = { "{ingredient name}": 0 };

  var dummy = _(products).chain()
     .pluck("ingredients")
     .flatten()
     .each(function(x) { ingredientCount[x] = (ingredientCount[x] || 0) + 1; })
     .value();

  expect(ingredientCount['mushrooms']).toBe(2);
});
于 2013-12-10T05:13:06.227 回答