javascript - How to generate an array using js.
巴扎黑
巴扎黑 2017-06-28 09:24:16
0
4
763

The array should be in the format of shuzu[{a:1,b:1},{a:2,b:2}].
Is to use a loop to generate key values ​​and value values

巴扎黑
巴扎黑

reply all(4)
習慣沉默
var arr = []
for (let i = 0; i < 10; i++) {
    arr.push({a: i, b: i})
}
console.log(arr);
过去多啦不再A梦

Owner, take a look

var arr=[]
for(let i=0;i<times;i++){
  let obj={}
  obj.a=i+1
  obj.b=i+1
  arrr.push(obj)
}
为情所困

Can be easily generated using fill and map of Array:

var arr = (new Array(2)).fill(0).map(function(v, i) {
    return {a: i + 1, b: i + 1};
});
黄舟
function createArray(n) {
    return Array.from(new Array(n), (x, i) => ({ a: i + 1, b: i + 1 }));
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template