How does `Array.from({length: 5}, (v, i) => i)` work?

后端 未结 4 1381
面向向阳花
面向向阳花 2020-12-24 12:48

I may be missing something obvious here but could someone breakdown step by step why Array.from({length: 5}, (v, i) => i) returns [0, 1, 2, 3, 4]

4条回答
  •  一个人的身影
    2020-12-24 13:16

    var arr1 = Array.from({
        length: 5 // Create 5 indexes with undefined values
      },
      function(v, k) { // Run a map function on said indexes using v(alue)[undefined] and k(ey)[0 to 4]
        return k; // Return k(ey) as value for this index
      }
    );
    console.log(arr1);

提交回复
热议问题