根據(jù)數(shù)組中的某個(gè)字段生成對應(yīng)字段值相同的二維數(shù)組

根據(jù)數(shù)組中的某個(gè)字段生成對應(yīng)字段值相同的數(shù)組,例:下面根據(jù)type這個(gè)字段來將type值一致的分到同一數(shù)組。。。

let arr = [
        {regulationsId: 5172, title: "測試111標(biāo)題2 ", type: 610, state: 1, createdTime: 1530152467000},
        {regulationsId: 5169, title: "測試111標(biāo)題", type: 610, state: 1, createdTime: 1530085573000},
        {regulationsId: 5170, title: "測試123標(biāo)題", type: 609, state: 1, createdTime: 1530085687000},
        {regulationsId: 5171, title: "測試1122標(biāo)題", type: 608, state: 1, createdTime: 1530085750000}
    ];
    
    //獲取數(shù)組中有多少個(gè)type
    let types = [];
    
    arr.map((item, index) => {
        if(types.indexOf(item.type) === -1){
            types.push(item.type)
        }
    })
    
    //一個(gè)包含多個(gè)list的結(jié)果對象
    let obj = [];
    
    // 根據(jù)type生成多個(gè)數(shù)組
    types.map((typeItem, typeIndex) => {
        arr.map((arrItem,  arrIndex) => {
            if(arrItem.type == typeItem){
                obj[typeIndex] = obj[typeIndex] || [];
                obj[typeIndex].push(arrItem)
            }
        })
    })

如果此文對你有用請動動你的小手點(diǎn)個(gè)贊!謝謝!??!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容