获取数组中下标为偶数的元素

本文为博主原创文章,未经博主允许不得转载。https://blog.csdn.net/sinat_38818576/article/details/86467259

var isEven = i => i % 2 === 0; 
var evens = arr => arr.filter(
	(subArr, idx) => isEven(idx)
); 
var flat = arr => arr.reduce((acc, cur) => {
	return acc.concat(cur)
}, []);
var getAllEvens = arr => {
	let temp = evens(arr);
	return flat(temp);
};
var testArr = ['这里', '是', '0', '号', '数组', '当然是偶数'];
var res = getAllEvens(testArr); 
console.log(res);

代码来源:https://segmentfault.com/q/1010000009629634?_ea=1995715

你可能感兴趣的:(获取数组中下标为偶数的元素)