我有以下数组:
var pageviews = [
[1, randValue()],
[2, randValue()],
[3, randValue()],
];
静态填充此数组。我该如何填写这个循环?
我试过了:
$.each(data, function(k, v) {
pageviews.push([num, v["value"]]);
})
但它给了我不同的结果
答案 0 :(得分:2)
请尝试:
var data = {a:"Hii", b:"bbye"}, // dummy data
pageViews = [];
$.each(data, function(key,val){
var temp = [];
temp.push(key,val)
pageViews.push(temp);
});
//output, pageViews = [["a","Hii"],["b","bbye"]]
如果您需要索引号,请将单独的计数器变量定义为:
var data = {a:"Hii", b:"bbye"}, // dummy data
pageViews = [],
counter = 1;
$.each(data, function(key,val){
var temp = [];
temp.push(counter++,val)
pageViews.push(temp);
});
//output, pageViews = [[1,"Hii"],[2,"bbye"]]