search

Home  >  Q&A  >  body text

javascript - How to generate an array using js.

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

巴扎黑巴扎黑2713 days ago778

reply all(4)I'll reply

  • 習慣沉默

    習慣沉默2017-06-28 09:26:16

    var arr = []
    for (let i = 0; i < 10; i++) {
        arr.push({a: i, b: i})
    }
    console.log(arr);

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-06-28 09:26:16

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

    reply
    0
  • 为情所困

    为情所困2017-06-28 09:26:16

    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};
    });

    reply
    0
  • 黄舟

    黄舟2017-06-28 09:26:16

    function createArray(n) {
        return Array.from(new Array(n), (x, i) => ({ a: i + 1, b: i + 1 }));
    }

    reply
    0
  • Cancelreply