LoginSignup
0
0

【javascript】【jquery】for不要論 Array.from({ length: 5 },を使う

Last updated at Posted at 2024-02-06

コード

Array.from({length:5},(_,i) =>
    console.log(i)
);

$.each([...Array(3).keys()], (i) => 
    console.log(i)
);

$.each([...Array(3).keys()], (i) => {
    console.log(i);
});

$.each([1,2,3,4,5],(_,i) =>
    console.log(i)
);

[...Array(5).keys()].forEach(i => 
	console.log(i)
);

const num = 15;

console.log([...Array(num).keys()]);

// 1から始めたいなら
console.log([...Array(num).keys()].map(n => ++n));

$.each([...Array(fields.length).keys()], function(_,i) {
    console.log(i);
})

結果

0
1
2
3
4
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0