J'ai ce tableau.

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

Est-il possible de le changer en

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

Si je peux comment boucler le tableau modifié en javascript?

0
Tanya von Degurechaff 8 août 2016 à 11:58

5 réponses

Meilleure réponse

Vous pouvez utiliser une combinaison de array.map et 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
Rajesh 8 août 2016 à 11:18

Essaye ça

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));
1
Krupesh Kotecha 8 août 2016 à 10:39

Version 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})
);
1
Maxx 8 août 2016 à 09:38

Vous pouvez le faire comme ci-dessous également: -

<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
Jitendra Pancholi 8 août 2016 à 09:58

Vous pouvez essayer quelque chose comme ça

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
V.pique 8 août 2016 à 09:09