开发者

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])})
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜