目前了解的解决办法
const oldList = [1, 2, 3, 4, 5, 6, 7]
// 使用reduce函数接受一个初始值{ 0: [], 1: [], length: 2 },
// 初始值包含两个空数组,和一个数组长度(Array.from方法要求将对象转数组时对象内要有这个属性)
// 在reduce函数内根据索引做余2判断,因为分两列,余0的加入第一个数组,余1的加入第二个数组
// 最后reduce返回遍历完的对象 {0:[1,3,5,7],1:[2,4,6],length:2}
// 使用Array.from({0:[1,3,5,7],1:[2,4,6],length:2}) 得到 数组 [[1,3,5,7],[2,4,6]]
// 解构数组 使用concat合并,完事
const newList = [].concat(...(Array.from(oldList.reduce((total, cur, index) => {
total[index % 2].push(cur)
return total
}, { 0: [], 1: [], length: 2 }))))
console.log(newList)
输出
[1, 3, 5, 7, 2, 4, 6]
然后再将两个数组合并,
总之就是我想将[1,2,3,4,5,6]拆成[1.3.5]和[2,4,6]然后再合并成
有什么最简洁,代码最少的方法将 [1,2,3,4,5,6]改为[1,3,5,2,4,6],
最好只遍历一次数组
java代码:
List里面的对象元素,以某个属性来分组,例如,以id分组,将id相同的放在一起:
复制代码
//List 以ID分组 Map<Integer,List<Apple>>
Map<Integer, List<Apple>> groupBy = appleList.stream().collect(Collectors.groupingBy(Apple::getId));
System.err.println("groupBy:"+groupBy);
{1=[Apple{id=1, name='苹果1', money=3.25, num=10}, Apple{id=1, name='苹果2', money=1.35, num=20}], 2=[Apple{id=2, name='香蕉', money=2.89, num=30}], 3=[Apple{id=3, name='荔枝', money=9.99, num=40}]}
9 回答
天涯尽头无女友
TA贡献1831条经验 获得超9个赞
这是题主按照答主思路写的一个思路一样,但实现不完全一样的符合题主要求的答案
const oldList = [1, 2, 3, 4, 5, 6, 7]
// 使用reduce函数接受一个初始值{ 0: [], 1: [], length: 2 },
// 初始值包含两个空数组,和一个数组长度(Array.from方法要求将对象转数组时对象内要有这个属性)
// 在reduce函数内根据索引做余2判断,因为分两列,余0的加入第一个数组,余1的加入第二个数组
// 最后reduce返回遍历完的对象 {0:[1,3,5,7],1:[2,4,6],length:2}
// 使用Array.from({0:[1,3,5,7],1:[2,4,6],length:2}) 得到 数组 [[1,3,5,7],[2,4,6]]
// 解构数组 使用concat合并,完事
const newList = [].concat(...(Array.from(oldList.reduce((total, cur, index) => {
total[index % 2].push(cur)
return total
}, { 0: [], 1: [], length: 2 }))))
console.log(newList)
输出
[1, 3, 5, 7, 2, 4, 6]
已下为答主提供的实现方式及思路,谢谢------------------------------------------------------
const arr = [1,2,3,4,5,6]
const newArr = [...arr.filter(e => e % 2), ...arr.filter(e => e % 2 === 0)]
其实也没必要追求这点性能, 既要代码少, 又要效率高... 我觉得乖乖写for循环最好了
嗯...
const obj = arr.reduce((a, b) => {
a[b%2].push(b)
return a
}, {0: [], 1: []})
const newArr = obj[1].concat(obj[0])
遍历一次呦 不知道合不合楼主口味
慕无忌1623718
TA贡献1744条经验 获得超4个赞
function arrSort(arr, newArr = []) {
arr.map((item, i, arr) => {
if (item % 2 === 0) {
newArr.push(item)
arr.splice(i, 1)
}
})
return arr.concat(newArr)
}
莫回无
TA贡献1865条经验 获得超7个赞
const groupBy = (original, fn) => {
if ((!Array.isArray(original)) || (!(fn instanceof Function))) throw Error('')
const fit = []
const unfit = []
original.forEach(el => {
fn(el) ? fit.push(el) : unfit.push(el)
})
return {fit, unfit}
}
groupBy([1,2,3,4,5,6],el=>{return el%2})//=>{fit:[1, 3, 5],unfit [2, 4, 6]}
慕虎7371278
TA贡献1802条经验 获得超4个赞
{
const arr = [1,2,3,4,5,6]
const even = []
const odd = arr.filter(item => {
if(item % 2 === 0){
even.push(item)
}
return item % 2 === 1;
})
console.log(odd);
console.log(even);
}
一只名叫tom的猫
TA贡献1906条经验 获得超3个赞
function groupBy( array , f ) {
let groups = {};
array.forEach( function( o ) {
let group = JSON.stringify( f(o) );
groups[group] = groups[group] || [];
groups[group].push( o );
});
return Object.keys(groups).map( function( group ) {
return groups[group];
});
}
基本用法
var arr = [1,2,3,4,5,6];
groupBy(arr,function(a){return a % 2 == 0;})
扩展用法
var arr = [{name:"a",age:2},{"name":"b",age:1},{"name":"c",age:1}];
groupBy(arr,function(a){return [a.age]})
暮色呼如
TA贡献1853条经验 获得超9个赞
很简单,用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];
添加回答
举报
0/150
提交
取消
