首页 新闻 会员 周边 捐助

求问一个关于AJAX返回的JSON数据转换处理的问题

0
悬赏园豆:50 [已解决问题] 解决于 2019-12-13 12:24

现有如下AJAX返回的JSON数据

result: Array(15)
0: {companystyle: "caa", userVipOnlineCount: 59}
1: {companystyle: "cab", userVipOnlineCount: 756}
2: {companystyle: "cac", userVipOnlineCount: 100}
3: {companystyle: "cad", userVipOnlineCount: 170}
4: {companystyle: "caf", userVipOnlineCount: 49}
5: {companystyle: "cag", userVipOnlineCount: 21}
6: {companystyle: "caj", userVipOnlineCount: 6}
7: {companystyle: "cak", userVipOnlineCount: 26}
8: {companystyle: "cam", userVipOnlineCount: 1066}
9: {companystyle: "can", userVipOnlineCount: 2077}
10: {companystyle: "cao", userVipOnlineCount: 7}
11: {companystyle: "cap", userVipOnlineCount: 9}
12: {companystyle: "cas", userVipOnlineCount: 11}
13: {companystyle: "cau", userVipOnlineCount: 3}
14: {companystyle: "test", userVipOnlineCount: 7}

如何转换成如下格式的
var companystyle = ["caa","cab","cac","cad",.......];

var userVipOnlineCount = [59,756,100,170......];

Rock7407的主页 Rock7407 | 初学一级 | 园豆:157
提问于:2019-12-05 17:40
< >
分享
最佳答案
1

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/map

var companystyle = result.map(_ => _.companystyle);
var userVipOnlineCount = result.map(_ => _.userVipOnlineCount);
收获园豆:50
Timetombs | 老鸟四级 |园豆:3959 | 2019-12-05 18:30

庸行天下 | 园豆:202 (菜鸟二级) | 2019-12-06 15:22
其他回答(4)
0

我的思路是:先获取这个长度 json 的length
然后for 循环 在 用两个集合 获取
for (var i = 0; i < data["countDsData"].length; i++) { //// 循环取出 服务前中后 的 状态图片数量有多少!

                    arr[data["countDsData"][i]] = data["countDsData"][i]["imgCount"];
                }

。。类似这样

永远跟党走i | 园豆:1542 (小虾三级) | 2019-12-05 17:53
0

var com=[],user=[];
var array=[{companystyle: "caa", userVipOnlineCount: 59}, {companystyle: "cab", userVipOnlineCount: 756}];
for(var i=0;i<array.length;i++){
com.push(array[i].companystyle);
user.push(array[i].userVipOnlineCount);
}

害羞的青蛙 | 园豆:202 (菜鸟二级) | 2019-12-06 09:39
0

可以使用map试试
var companystyle_data=result.map(function (d) { return d.companystyle; });
var userVipOnlineCount_data=result.map(function (d) { return d.userVipOnlineCount; });

庸行天下 | 园豆:202 (菜鸟二级) | 2019-12-06 15:23
0

for (var i=0;i<Array.length;i++){
company[i] = Array[i].companystle
user[i]= Array[i].user
}

老王同鞋 | 园豆:208 (菜鸟二级) | 2019-12-13 00:43
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册