我有这个阵列。
[[" 20152"" 15301"" 020053"" 2016年8月5日"" 05 :00"" 06:00"" 07150100190"],[" 20152"" 15301"" 020051"" 2016年8月5日"" 03:00"" 04:00"" 07150100190&#34], [" 20152"" 15301"" 000040"" 2016年8月5日"" 01:00&#34 ;," 02:00"" 07150100190"],[" 20152"" 15301"" 020050" " 2016年8月5日"" 07:00"" 08:00"" 07150100190"]]
是否可以将其更改为
var timetable = [{
"thakad" : "20152",
"prodi" : "15301",
"mk" : "020053",
"date" : "2016-08-05",
"open" : "05:00",
"close" : "06:00",
"number" : "07150100190"
},
{
//and keep looping dynamically
}];
如果我可以如何在javascript中循环更改数组?
答案 0 :(得分:1)
您可以尝试这样的事情
var array = [["20152","15301","020053","2016-08-05","05:00","06:00","07150100190"],["20152","15301","020051","2016-08-05","03:00","04:00","07150100190"],["20152","15301","000040","2016-08-05","01:00","02:00","07150100190"],["20152","15301","020050","2016-08-05","07:00","08:00","07150100190"]]
var elems = ["thakad","prodi","mk","date","open","close","number"]
var array = array.map(function(arg){
return (function(){
var obj = {};
for (var i = 0;i < elems.length; i++) {
obj[elems[i]] = arg[i];
}
return obj;
})()
});
array = JSON.stringify(array);
console.log(array);
答案 1 :(得分:1)
你也可以这样做: -
<script type="text/javascript">
function test() {
var json = [["20152", "15301", "020053", "2016-08-05", "05:00", "06:00", "07150100190"], ["20152", "15301", "020051", "2016-08-05", "03:00", "04:00", "07150100190"], ["20152", "15301", "000040", "2016-08-05", "01:00", "02:00", "07150100190"], ["20152", "15301", "020050", "2016-08-05", "07:00", "08:00", "07150100190"]];
var arr = [];
$(json).each(function (i) {
arr.push({
thakad: json[i][0],
prodi: json[i][1],
mk: json[i][2],
date: json[i][3],
open: json[i][4],
close: json[i][5],
number: json[i][6]
});
});
console.log(JSON.stringify(arr));
}
</script>
答案 2 :(得分:1)
ES2015版本:
const data = [["20152","15301","020053","2016-08-05","05:00","06:00","07150100190"],["20152","15301","020051","2016-08-05","03:00","04:00","07150100190"],["20152","15301","000040","2016-08-05","01:00","02:00","07150100190"],["20152","15301","020050","2016-08-05","07:00","08:00","07150100190"]]
const arr = data.map(([thakad, prodi, mk, date, open, close, number]) =>
({thakad, prodi, mk, date, open, close, number})
);
答案 3 :(得分:1)
试试这个
var t = [["20152","15301","020053","2016-08-05","05:00","06:00","07150100190"],["20153","15301","020051","2016-08-05","03:00","04:00","07150100190"],["20154","15301","000040","2016-08-05","01:00","02:00","07150100190"],["20155","15301","020050","2016-08-05","07:00","08:00","07150100190"]];
var res=[];
var keyArray =['thakad','prodi','mk','date','open','close','number'];
for(var i=0;i<t.length;i++){
var temp={};
for(var j=0;j<t[i].length;j++){
temp[keyArray[j]] = t[i][j];
res[i] = temp;
}
}
console.log(JSON.stringify(res));
答案 4 :(得分:1)
您可以使用array.map
和array.reduce
var array = [["20152","15301","020053","2016-08-05","05:00","06:00","07150100190"],["20152","15301","020051","2016-08-05","03:00","04:00","07150100190"],["20152","15301","000040","2016-08-05","01:00","02:00","07150100190"],["20152","15301","020050","2016-08-05","07:00","08:00","07150100190"]]
var keys = ["thakad","prodi","mk","date","open","close","number"]
var r = array.map(function(el){
return keys.reduce(function(c,n,i){
c[n] = el[i]
return c;
},{});
});
console.log(r)