I have an array like this:
var arr = [
['a1','b1','c1','d1'],
['a1','e1','i1','j1'],
['a1','f1','k1','l1'],
['a1','g1','m1','n1'],
['a1','h1','o1','p1'],
['a2','b2','c2','d2'],
['a2','e2','i2','j2'],
['a2','f2','k2','l2'],
['a2','g2','m2','n2'],
['a2','h2','o2','p2']
];
and I want to convert it to an object like this:
var converted_arr = {
'a1':{
'b1':['c1','d1'],
'e1':['i1','j1'],
'f1':['k1','l1'],
'g1':['m1','n1'],
'h1':['o1','p1'],
},
'a2':{
'b2':['c2','d2'],
'e2':['i2','j2'],
'f2':['k2','l2'],
'g2':['m2','n2'],
'h2':['o2','p2'],
}
};
As you can see all the items of the first array (arr) are arrays and it's item in these arrays are strings or numbers or both.
One of the examples I have tried is the following:
var obj = {};
$.each(arr,function(a)
{
obj[a[0]][a[1]] = a.slice(2);
});
But it produces nothing.
Can someone help me out?
converted_arris a poor choice becausearrsuggests that the value is an array when actually it is an object.convertedwould be a poor choice because it's essentially meaningless. Choose a name that's indicative of the purpose/meaning of the value for your final solution. Good luck!