javascript | 提取JSON值为纯数组,键为title

#需求

Json对象提取值储存纯数组,提取键做title,为上一文章导出Excel作数据

#javascript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
*
* @param { JSONObject:[obj,obj,obj] }
* @return { Array }
*
*/
function JSONToArray(){};
JSONToArray.prototype = {
count: 0,
_init: function (data){
console.log(data)
if(navigator.userAgent.indexOf('MSIE') > 0){
this._getIEObjLength(data);
return this._go(data);
}else {
this.count = Object.keys(data[0]).length;
console.log(this._go(data))
return this._go(data);
}
},
_go: function (data){
var valArr = new Array();
var keyArr = new Array();
for (var i = 0; i < data.length; i++) {
valArr[i] = [];
for(var k in data[i]){
valArr[i].push(data[i][k]);
if(keyArr.length < this.count){
keyArr.push(k);
}
}
}
valArr.unshift(keyArr);
return valArr;
},
_getIEObjLength: function (data){
for(var o in data[0]){
this.count ++;
}
}
}

#结果

1
2
3
4
5
6
7
8
9
10
11
12
//原数据
[
{"id":10000,"username":"user-0","sex":"女","city":"城市-0","sign":"签名-0","experience":255,"logins":24},
{"id":10001,"username":"user-1","sex":"男","city":"城市-1","sign":"签名-1","experience":884,"logins":58},
]

//处理后
[
["id","username","sex","city","sign","experience","logins"],
[10000,"user-0","女","城市-0","签名-0",255,24],
[10001,"user-1","男","城市-1","签名-1",884,58]
]

兼容ie6-11