gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:37:25 26 4
gpt4 key购买 nike

请找到我的 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 以上述格式形成数据,如果有人可以帮助实现这一点,那将非常有帮助。

谢谢!

[ {
"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
} ]

最佳答案

据我了解,您想从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);

关于javascript - 如何在javascript中从JSON数组创建值数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32516216/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com