maps函数在控制台上提供6个数组,并且不会输出单独的数组

时间:2019-01-17 07:40:28

标签: javascript

我仍在尝试在map函数中导航,但仍需要一点帮助。

我试图将所有storeArea值收集到一个单独的数组中,并将所有totalStore.date值收集到不同的单独数组中。 当我使用map时,我在一个对象中获取totalStore.date 6个数组。什么是正确和正确的事情? 谢谢!

var results =[]

var stores_json = {
"stores": [
  {
    "storeArea": "area1",
    "totalStore": [
      {
        "date": "2018-10-01"
      },
      {
        "date": "2018-11-01"
      },
      {
        "date": "2018-12-01"
      }
    ]
  },
  {
    "storeArea": "area2",
    "totalStore": [
      {
        "date": "2018-10-01"
      },
      {
        "date": "2018-11-01"
      },
      {
        "date": "2018-12-01"
      }
    ]
  },
  {
    "storeArea": "area3",
    "totalStore": [
      {
        "date": "2018-10-01"
      },
      {
        "date": "2018-11-01"
      },
      {
        "date": "2018-12-01"
      }
    ]
  },
  {
    "storeArea": "area4",
    "totalStore": [
      {
        "date": "2018-10-01"
      },
      {
        "date": "2018-11-01"
      },
      {
        "date": "2018-12-01"
      }
    ]
  },
  {
    "storeArea": "area5",
    "totalStore": [
      {
        "date": "2018-10-01"
      },
      {
        "date": "2018-11-01"
      },
      {
        "date": "2018-12-01"
      }
    ]
  },
  {
    "storeArea": "area6",
    "totalStore": [
      {
        "date": "2018-10-01"
      },
      {
        "date": "2018-11-01"
      },
      {
        "date": "2018-12-01"
      }
    ]
  }
]
}

results = stores_json["stores"].map(function (x) {
    return [x.storeArea, x.totalStore.map(function (y) {
        var dates = [y.date]
        return dates
    })]
});

1 个答案:

答案 0 :(得分:1)

areas = json.stores.map(function (x) {
    return x.storeArea
});
dates = json.stores.map(function (x) {
    return x.totalStore.map(function (y) {
        return y.date
    })
});
results = [areas,dates.flat()]

结果是-

(2) [Array(6), Array(18)]
0: (6) ["area1", "area2", "area3", "area4", "area5", "area6"]
1: (18) ["2018-10-01", "2018-11-01", "2018-12-01", "2018-10-01", "2018-11-01", "2018-12-01", "2018-10-01", "2018-11-01", "2018-12-01", "2018-10-01", "2018-11-01", "2018-12-01", "2018-10-01", "2018-11-01", "2018-12-01", "2018-10-01", "2018-11-01", "2018-12-01"]