现有如下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......];
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/map
var companystyle = result.map(_ => _.companystyle);
var userVipOnlineCount = result.map(_ => _.userVipOnlineCount);
顶
我的思路是:先获取这个长度 json 的length
然后for 循环 在 用两个集合 获取
for (var i = 0; i < data["countDsData"].length; i++) { //// 循环取出 服务前中后 的 状态图片数量有多少!
arr[data["countDsData"][i]] = data["countDsData"][i]["imgCount"];
}
。。类似这样
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);
}
可以使用map试试
var companystyle_data=result.map(function (d) { return d.companystyle; });
var userVipOnlineCount_data=result.map(function (d) { return d.userVipOnlineCount; });
for (var i=0;i<Array.length;i++){
company[i] = Array[i].companystle
user[i]= Array[i].user
}