Javascript——数据处理

//相当于调同一个接口,参数不一样调三次接口,
let arr1 = [];
      newArray.forEach((v, index) => {
        let sqlParams = `select * from alarm_center_info where 1=1 and model_name = '${v.newModel}' and period_time <= '${v.newTime}' and period_time > '${v.minTime}'`;
      arr1[index] = req(sqlParams);
      });

 let firstData = [];
 let secondData = [];
 let threedData = [];
      Promise.all(arr1).then(function(data) {
        //data 返回数组对象[0:{...},1:{..},2:{..}]
        //根据对象里某一个字段区分开三种不同类型的数据
        data.forEach(item => {
           if (item.rows.length !== 0) {
            item.rows.forEach(v => {
              if (
                v.model_name == 'first' 
              ) {
                firstData.push(v);
              } else if (
                v.model_name == 'tow' 
              ) {
                secondData.push(v);
              } else if (v.model_name == 'three') {
                threedData.push(v);
              }
            });
          }
        })
console.log(firstData, '三种不同类型的数据', secondData,  threedData);
//返回结果[{object},{object},{object}.....],[{object}....],[{object},{object}.....]

           function findDuplicateObjects(arr) {
          //根据每个对象里面某一个字段(min_name)判断是否有重复的数据
          let newArr = [];
          arr.forEach(item => {
  //findIndex() 方法返回传入一个测试条件(函数)符合条件的数组第一个元素位置。为数组中的每个元素都调用一次函数执行
  //当数组中的元素在测试条件时返回 true 时, findIndex() 返回符合条件的元素的索引位置,之后的值不会再调用执行函数。
  //如果没有符合条件的元素返回 -1
            let newArrIndex = newArr.findIndex(v => v.name === item.min_name);
            if (newArrIndex === -1) {
              let newArrObj = arr.filter(v => v.name == item.min_name);
              newArr.push({ name: item.min_name, value: newArrObj });
              
            }
          });
          return newArr;
        }
        let oneRepeatList = findDuplicateObjects(firstData);
        let towRepeatList = findDuplicateObjects(secondData);
        let threeRepeatList = findDuplicateObjects(threedData);

        console.log(oneRepeatList,towRepeatList, threeRepeatList, '1111',);
        //返回结果:oneRepeatList:[
         { name:'1',value:[ { id:'1',name:'1',time:'2023-12-09'},{ id:'2',name:'1',time:'2023-01-09'},{},{},{}.....]},
         {{ name:'2',value:[ { id:'0021',name:'1',time:'2023-10-09'},{ id:'2',name:'1',time:'2023-01-09'},{},{},{}.....},
         {{ name:'3',value:[ { id:'3331',name:'1',time:'2023-12-09'},{ id:'3332',name:'1',time:'2023-06-09'},{},{},{}.....}
         .... 
         ]
       

     

     
        
        
      })

你可能感兴趣的:(javascript,前端)