如何在javascript中从JSON数组创建值数组

时间:2015-09-11 05:39:51

标签: javascript arrays json

请找到我的json数组,我想以下面的格式形成数据数组:

[Sunday.a_count.value,Monday.a_count.value,Tuesday.a_count.value,Wednesday.a_count.value] - 此格式输出将 [0,2,2,3] -

最终,这些值来自星期日a_count,星期一a_count,星期二a_count和星期三a_count。

我无法在javascript中使用json下面的json形成上述格式的数据,如果有人可以帮助实现这一点,那将非常有帮助。

谢谢!

[ {
  "day" : "Sunday",
  "a_count" : 0,
  "b_count" : 2,
  "c_count" : 0,
  "d_count" : 0
}, {
  "day" : "Monday",
  "a_count" : 2,
  "b_count" : 0,
  "c_count" : 0,
  "d_count" : 0
}, {
  "day" : "Tuesday",
  "a_count" : 2,
  "b_count" : 0,
  "c_count" : 0,
  "d_count" : 0
}, {
  "day" : "Wednesday",
  "a_count" : 3,
  "b_count" : 0,
  "c_count" : 0,
  "d_count" : 0
} ]

1 个答案:

答案 0 :(得分:2)

根据我的理解,您希望从a_count获得价值,您可以使用.map来实现,就像这样



var data = [ {
  "day" : "Sunday",
  "a_count" : 0,
  "b_count" : 2,
  "c_count" : 0,
  "d_count" : 0
}, {
  "day" : "Monday",
  "a_count" : 2,
  "b_count" : 0,
  "c_count" : 0,
  "d_count" : 0
}, {
  "day" : "Tuesday",
  "a_count" : 2,
  "b_count" : 0,
  "c_count" : 0,
  "d_count" : 0
}, {
  "day" : "Wednesday",
  "a_count" : 3,
  "b_count" : 0,
  "c_count" : 0,
  "d_count" : 0
} ];

var res = data.map(function (el) {
  return el.a_count;
});

console.log(res);