constructing two dimensional array dynamically
I have some values in JavaScript array as shown
var sampledata = {10,20,30,40};// these values would com开发者_StackOverflow社区e from database later
I want to create a two dimensional array with these values.
I want to create a array as
var newData = [[0,10],[1,20],[2,30],[3,40]]
Pure JavaScript:
var newData = [];
var sampledata = [10,20,30,40];
for (var i = 0; i < sampledata.length; i++) {
newData.push([i, sampledata[i]]);
}
Using higher-order functions:
var newData = sampledata.map(function(el, i) {
return [i, el];
})
if sampledata is an array
var sampledata = [10,20,30,40]
var newData = []
jQuery.each(sampledata,function(i,data){newData.push([i,data])})
精彩评论