How to convert an array to associative? I write the following:
arr=[one,two,three] res=[] $.each(arr,function(idx,el){ idx+=1 res.push(idx,el) }) should be like this [1:one,2:two,3:three] and I have [1,one,2,two,3,three]
How to convert an array to associative? I write the following:
arr=[one,two,three] res=[] $.each(arr,function(idx,el){ idx+=1 res.push(idx,el) }) should be like this [1:one,2:two,3:three] and I have [1,one,2,two,3,three]
Your code simply inserts several elements into the array. To get [{1: one}, {2: two}, {3: three}] you need to insert already prepared objects:
arr=['one','two','three']; var res=[]; $.each(arr,function(idx,el) { var item = {}; item[idx+1] = el; res.push(item); }) It is much more convenient to use not an array with objects, but an object with index properties
{ 1: one, 2: two, 3: three, } To get such an object, you need to use the following code:
var arr= ['one', 'two', 'three']; var res = {}; $.each(arr,function(idx,el){ res[idx+1] = el; }) Source: https://ru.stackoverflow.com/questions/527329/
All Articles
unshiftfor anunshiftelement and the indexes will align - Grundy