📜  js 数组仅返回特定位置 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:30.861000             🧑  作者: Mango

代码示例1
const every_nth = (arr, nth) => arr.filter((e, i) => i % nth === nth - 1);
console.log(every_nth([1, 2, 3, 4, 5, 6], 1));
console.log(every_nth([1, 2, 3, 4, 5, 6], 2));
console.log(every_nth([1, 2, 3, 4, 5, 6], 3));
console.log(every_nth([1, 2, 3, 4, 5, 6], 4));

// OUTPUT

[1,2,3,4,5,6]
[2,4,6]
[3,6]
[4]