zl程序教程

您现在的位置是:首页 >  后端

当前栏目

js数组根据对象中的某key值,组成新数组,然后去重 ( 示例代码 001)

JS对象数组代码 示例 Key 根据 组成
2023-09-14 09:15:35 时间

本示例是处理数组中的对象中的数组,有点绕,看示例就知道怎么回事了。

专栏目录

JS应用示例100+ 目录

项目背景:

一个数组中包括了多个对象,每个对象中又包括数组等因子,总之很复杂。 要求是将数组中的对象中的数组集中在一起,行程新的数组,然后去重。

项目效果:在这里插入图片描述

项目代码:

let array = [{ 
        date: '2022-05-02', 
        name: 'cuclife', 
        status: 'success', 
        tag: '70', 
        band: ['R', 'B', 'PE'] 
    }, 
    { 
        date: '2022-05-04', 
        name: 'Lily', 
        status: 'fail', 
        tag: '50', 
        band: ['R', 'G'] 
    }, 
    { 
        date: '2022-05-01', 
        name: 'Kevin', 
        status: 'success', 
        tag: '20', 
        band: ['R', 'G', 'B'] 
    }, 
    { 
        date: '2022-05-02', 
        name: 'Kevin', 
        status: 'success', 
        tag: '70', 
        band: ['R', 'G', 'B', 'RE'] 
    }, 
]; 
 
//将数组对象中的某值组成新的数组 
function composeArray(array) { 
    let params = []; 
    for (let items of array) { 
        params.push(items.band); 
    } 
     
    let params1 = [] 
    for (let j = 0; j < params.length; j++) { 
        params1 = [...params1, ...params[j]] 
    } 
 
    var newArr = params1.filter(function(item, index) { 
        return params1.indexOf(item) === index;  
     }); 
    return newArr 
    console.log(newArr) 
} 
 
composeArray(array);