使用.join映射数组时遇到问题

时间:2019-07-17 17:19:52

标签: javascript html arrays mapping filtering

我无法映射数组的击球员部分。作业要求:“ batters”:“所有击球员类型都作为字符串。我无法在没有多个数组的情况下进行映射。目标是显示击球员,例如

面糊:“常规”,“巧克力”,“蓝莓”

我尝试过数组的.join方法必须显示为字符串,从而不断出错。

sabio.page.startUp = function () {
  console.log(oldCakesObject);

  let newCakes = oldCakesObject.items.item.map(sabio.page.mapCakes);
  console.log(newCakes);
}

sabio.page.mapCakes = function (cake) {
  let numberOfFillings = 0;
  let batters = batter.join()

  if (cake.fillings) {
    numberOfFillings = cake.fillings.filling.length
  }
  return {
    id: cake.id,
    type: cake.type,
    name: cake.name,
    ppu: cake.ppu,
    // batters: cake.items.batter,
    ingredients: cake.topping,
    countOfFillings: numberOfFillings
  }
};

{
  "items":
  {
    "item":
    [
      {
        "id": "0001",
        "type": "donut",
        "name": "Cake",
        "ppu": 0.55,
        "batters":
        {
          "batter":
            [
              { "id": "1001", "type": "Regular" },
              { "id": "1002", "type": "Chocolate" },
              { "id": "1003", "type": "Blueberry" },
              { "id": "1004", "type": "Devil's Food" }
            ]

尝试console.log击球手时出现错误。唯一的问题是击球手。

1 个答案:

答案 0 :(得分:0)

在使用type之前,您需要将对象的面糊数组映射到仅join()个值的数组

let batters = cake.batters.batter.map(item => item.type).join()