用几种方法可以将[1,2,3,4,5,6,7,8]修改成[1,3,5,7,2,4,6,8]
方法一:循环方法map
function arrSort(arr, newArr = []) {
arr.map((item, i, arr) => {
if (item % 2 === 0) {
newArr.push(item)
arr.splice(i, 1)
}
})
return arr.concat(newArr)
}
方法二:es6筛选器filter
const baseArray = [1,2,3,4,5,6];
const oddArray = baseArray.filter(number => number % 2 === 1);
const evenArray = baseArray.filter(number => number % 2 === 0);
const finalArray = [...oddArray, ...evenArray];
网友评论