数组对象 如果id值相同,id下的data数组,合并在一起

 //例子 [{id:1,data:[12.1]},{id:1,data:[12.2]}]

                    var arr = []
                    var factorListData = []
                    var newFactorList = []
                    data.forEach( item =>  {
                        arr.push( item.recordDate)
                        if(item.factorList && item.factorList.length > 0) newFactorList.push(...item.factorList)
                    })   
                    newFactorList.forEach( item => {
                        factorListData.push({
                            name:item.name,
                            data: [item.monitorValue],                           
                        })
                    })
                    var color = ['#fab6b8','#dceced','#50dbbf','#f7b289']
                    function mergeDuplicateArrays(objects) {
                        let mergedObjects = {};
                        objects.forEach(obj => {
                            if (mergedObjects[obj.name]) {
                                mergedObjects[obj.name].push(...obj.data);
                            } else {
                                mergedObjects[obj.name] = [...obj.data];
                            }
                        });
                        return Object.entries(mergedObjects).map(([name, data]) =>                                     (
                            { 
                                name, 
                                data,
                                type: "line",
                                // stack: 'Total',
                                smooth:true,
                                itemStyle:{
                                    normal:{
                                        color:''
                                    }

                                }
                            showSymbol:false,
                            }
                           })

                    }

//函数运行以后 结果 [{id:1,data:[12.1,12.2]}]

你可能感兴趣的:(javascript,前端,开发语言)