Array合并

今晚打老虎
  • 2.1k

from

[
    {
        name: 'Lee',
        class: '002',
        number: 1
    },
    {
        name: 'Lee',
        class: '003',
        number: 3
    },
    {
        name: 'Karina',
        class: '001',
        number: 2
    }
]

to

[
    {
        name: 'Lee',
        info: [
            {
                class: '001',
                number: 1
            },
            {
                class: '003',
                number: 3
            }
        ]
    },
    {
        name: 'Karina',
        info: [
            {
                class: '001',
                number: 2
            }
        ]
    }
]

怎么实现?

回复
阅读 2.6k
6 个回答

亲测可以,只要给的数据按姓名排好

            var emm=[];var k=0;var msl = oarr[0].name;
            $.each(oarr,function(i,j){
                if(i){
                    if(msl!=j.name) k++;
                } 
                if(!emm[k]){
                    emm[k]={};
                    
                }
                
                emm[k]['name']=j.name;

                if(!emm[k]['info']) {
                    emm[k]['info']=[];
                }
                delete j.name;
                emm[k]['info'].push(j);
                
            })
        
        console.log(emm)
var obj = {} , arr = [];
a.forEach(function( item ){
    var _o = {};
    for( p in item ){
        if( p !== "name"){
            _o[p] = item[p];
        }
    }
    if( item.name in obj ){
        obj[item.name].push(_o);
    }else{
        obj[item.name] = [_o];
    }
});
for( p in obj ){
    arr.push({ "name" : p , "info" : obj[p] });
}

a是那个原始数组

苏福
  • 167

帮你写了个函数

function together(oldArr){
    var newArr = [];
    for(var i = 0; i<oldArr.length; i++){ //这里不能缓存length,因为它将变动
        var obj = {name:'', info:[]},arr;
        obj.name = oldArr[i].name;
        obj.info.push({class: oldArr[i].class, number: oldArr[i].number});
        for(var j = i+1;j<oldArr.length;j++){
            if(oldArr[j].name === oldArr[i].name){
                arr = oldArr.splice(j--,1);
                obj.info.push({class: arr[0].class, number: arr[0].number})
            }
        }
        newArr.push(obj);
    }
    return newArr
}
cj0x39e
  • 70
var sourceArray = [
    {
        name: 'Lee',
        class: '002',
        number: 1
    },
    {
        name: 'Lee',
        class: '003',
        number: 3
    },
    {
        name: 'Karina',
        class: '001',
        number: 2
    }
];
var resultArray = sourceArray.sort(function (objOne, objTwo){
   return objOne.name === objTwo.name;
}).reduce(function(rest, objItem, index){
   if(index === 0 || rest[rest.length - 1].name !== objItem.name){
      rest.push({
          name: objItem.name,
        info: [{ class: objItem.class, number: objItem.number }]
      });
    } else {
      rest[rest.length - 1].info.push({ class: objItem.class, number: objItem.number });
    }
    return rest;
}, []);

// 输出结果
console.log(resultArray)

就用 reduce 解决吧

const from = [
    {
        name: "Lee",
        class: "002",
        number: 1
    },
    {
        name: "Lee",
        class: "003",
        number: 3
    },
    {
        name: "Karina",
        class: "001",
        number: 2
    }
];

const to = from.reduce((r, t) => {
    let info = r.infoMap[t.name];

    if (!info) {
        info = [];
        r.list.push({
            name: t.name,
            info: info
        });
        r.infoMap[t.name] = info;
    }

    info.push({
        class: t.class,
        number: t.number
    });

    return r;
}, { list: [], infoMap: [] }).list;

console.log(JSON.stringify(to, null, 4));

forEach 可能更容易理解一点,顺便把关键算法换个方法

const to = ((data) => {
    const list = [];
    const infoMap = {};

    data.forEach(t => {
        let info = infoMap[t.name] = infoMap[t.name] || [];
        info.push({
            class: t.class,
            number: t.number
        });

        if (info.length === 1) {
            list.push({
                name: t.name,
                info: info
            });
        }
    });

    return list;
})(from);

newArr = arr1.concat(arr2)

宣传栏