【问题标题】:Having issues with mapping an array with .join使用 .join 映射数组时遇到问题
【发布时间】:2019-11-26 14:29:18
【问题描述】:

我无法映射阵列的击球手部分。作业要求:“击球手”:“所有击球手类型为字符串。如果没有多个数组,我无法映射它。目标是让击球手显示,例如

面糊:“普通”、“巧克力”、“蓝莓”

我已经尝试过数组的 .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 击球手时遇到错误。唯一的问题是击球手。

【问题讨论】:

    标签: javascript html arrays mapping filtering


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-06
      • 2017-06-26
      • 1970-01-01
      • 2012-03-04
      • 1970-01-01
      • 1970-01-01
      • 2021-01-04
      • 1970-01-01
      相关资源
      最近更新 更多