0

i have this 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"]]

is it possible to change it into

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

If i can how do i loop changed array in javascript?

1
  • 1
    Well. Yes. You loop over it. You create an object each loop. What about those two very basic bits of JavaScript is causing you problems? Commented Aug 8, 2016 at 9:02

5 Answers 5

1

You can use combination of array.map and 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)

Sign up to request clarification or add additional context in comments.

Comments

1

You can try something like this

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

Comments

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

Comments

1

You can do it like below also:-

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

Comments

1

Try this

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.