2016-08-08 35 views
0

Ich habe dieses Array.Kann ich dieses Looping-Array in Json Object umwandeln?

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

ist es möglich, sie in

var timetable = [{ 
    "thakad" : "20152", 
    "prodi" : "15301", 
    "mk"  : "020053",   
    "date" : "2016-08-05", 
    "open" : "05:00", 
    "close" : "06:00", 
    "number" : "07150100190" 
}, 
{ 
    //and keep looping dynamically 
}]; 

zu ändern, wenn i wie i-Array in JavaScript geändert Schleife kann?

+1

Nun verwenden. Ja. Du fährst darüber hinweg. Sie erstellen für jede Schleife ein Objekt. Was ist mit diesen zwei grundlegenden Bits von JavaScript verursacht Ihnen Probleme? – Quentin

Antwort

1

Sie können Kombination von array.map und 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)

1

Sie können so etwas wie dieses

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

Sie versuchen, es wie unten auch tun können: -

<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> 
1

ES2015 Version:

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}) 
); 
1

Versuchen Sie, diese

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));