码迷,mamicode.com
首页 > 编程语言 > 详细

数组遍历对象的item去掉某一个item对象变成null

时间:2020-07-10 13:04:03      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:nts   oss   com   rate   ide   tco   判断   put   去掉   

var  arr=[{obj:‘1‘},{obj:‘2‘}]
类似以上数组进行条件判断之后变成   [{obj:‘1‘,null}]  这种数组。

遍历查找arr的lostpower.lostList下item中所有item的power都是null的项对于arr的索引集合。

const arr = [
      {
        ‘date‘: ‘2020-04-19‘,
        ‘genIntegrated‘: null,
        ‘complantPowerRate‘: null,
        ‘dayPowerMaxTime‘: null,
        ‘stationAvailability‘: null,
        ‘genPlantConsume‘: null,
        ‘integratedLostEH‘: ‘0.0000‘,
        ‘ggg‘: null,
        ‘carbonDioxide‘: ‘647376000.0000‘,
        ‘comPlantPower‘: null,
        ‘slopeAccRadiationSum‘: ‘24.32‘,
        ‘plantLossRate‘: null,
        ‘genInternet‘: null,
        ‘resourceInditor‘: null,
        ‘total‘: ‘0.8928‘,
        ‘theoryPower‘: ‘34483.5876‘,
        ‘topSunshineHours‘: ‘6.7562‘,
        ‘pvMatrixLossHours‘: ‘0.0000‘,
        ‘yyy‘: null,
        ‘yyyyy‘: null,
        ‘stationName‘: ‘永仁‘,
        ‘genInverter‘: ‘25.8950‘,
        ‘id‘: 20200419,
        ‘buyPower‘: null,
        ‘inverterLostEH‘: ‘0.0000‘,
        ‘resourceValue‘: ‘0.03‘,
        ‘comPR‘: null,
        ‘plantPowerRate‘: null,
        ‘markCoal‘: ‘259729588.7663‘,
        ‘internetLostEH‘: ‘0.0000‘,
        ‘powerInditor‘: null,
        ‘jjjj‘: null,
        ‘outputPowerMax‘: ‘43.92‘,
        ‘lostList‘: [
          {
            ‘name‘: ‘光伏设备缺陷‘,
            ‘power‘: null,
          },
          {
            ‘name‘: ‘其他未查明‘,
            ‘power‘: ‘0.1‘,
          },
          {
            ‘name‘: ‘厂外因素‘,
            ‘power‘: null,
          },
          {
            ‘name‘: ‘变电设备缺陷‘,
            ‘power‘: null,
          },
          {
            ‘name‘: ‘外部因素‘,
            ‘power‘: null,
          },
          {
            ‘name‘: ‘未查明降容‘,
            ‘power‘: ‘0.2‘,
          },
          {
            ‘name‘: ‘计划停运‘,
            ‘power‘: ‘0.3‘,
          },
          {
            ‘name‘: ‘设备故障‘,
            ‘power‘: ‘0.4‘,
          },
          {
            ‘name‘: ‘限电‘,
            ‘power‘: null,
          },
        ],
        ‘ttt‘: null,
        ‘equivalentHours‘: ‘5.0735‘,
        ‘dayPlantUsePower‘: null,
        ‘lostPower‘: {
          ‘total‘: ‘0.8928‘,
          ‘lostList‘: [
            {
              ‘name‘: ‘光伏设备缺陷‘,
              ‘power‘: null,
            },
            {
              ‘name‘: ‘其他未查明‘,
              ‘power‘: ‘0.2232‘,
            },
            {
              ‘name‘: ‘厂外因素‘,
              ‘power‘: null,
            },
            {
              ‘name‘: ‘变电设备缺陷‘,
              ‘power‘: null,
            },
            {
              ‘name‘: ‘外部因素‘,
              ‘power‘: null,
            },
            {
              ‘name‘: ‘未查明降容‘,
              ‘power‘: ‘0.2232‘,
            },
            {
              ‘name‘: ‘计划停运‘,
              ‘power‘: ‘0.2232‘,
            },
            {
              ‘name‘: ‘设备故障‘,
              ‘power‘: null,
            },
            {
              ‘name‘: ‘限电‘,
              ‘power‘: null,
            },
          ],
        },
      },
      {
        ‘date‘: ‘2020-04-20‘,
        ‘genIntegrated‘: null,
        ‘complantPowerRate‘: null,
        ‘dayPowerMaxTime‘: null,
        ‘stationAvailability‘: null,
        ‘genPlantConsume‘: null,
        ‘integratedLostEH‘: ‘0.0000‘,
        ‘ggg‘: null,
        ‘carbonDioxide‘: ‘689398750.0000‘,
        ‘comPlantPower‘: null,
        ‘slopeAccRadiationSum‘: ‘26.31‘,
        ‘plantLossRate‘: null,
        ‘genInternet‘: null,
        ‘resourceInditor‘: null,
        ‘total‘: ‘0.8928‘,
        ‘theoryPower‘: ‘37296.3918‘,
        ‘topSunshineHours‘: ‘7.3073‘,
        ‘pvMatrixLossHours‘: ‘0.0000‘,
        ‘yyy‘: null,
        ‘yyyyy‘: null,
        ‘stationName‘: ‘永仁‘,
        ‘genInverter‘: ‘27.5760‘,
        ‘id‘: 20200420,
        ‘buyPower‘: null,
        ‘inverterLostEH‘: ‘0.0000‘,
        ‘resourceValue‘: ‘0.03‘,
        ‘comPR‘: null,
        ‘plantPowerRate‘: null,
        ‘markCoal‘: ‘276589267.8034‘,
        ‘internetLostEH‘: ‘0.0000‘,
        ‘powerInditor‘: null,
        ‘jjjj‘: null,
        ‘outputPowerMax‘: ‘41.54‘,
        ‘lostList‘: [
          {
            ‘name‘: ‘光伏设备缺陷‘,
            ‘power‘: ‘0.2232‘,
          },
          {
            ‘name‘: ‘其他未查明‘,
            ‘power‘: null,
          },
          {
            ‘name‘: ‘厂外因素‘,
            ‘power‘: ‘0.2232‘,
          },
          {
            ‘name‘: ‘变电设备缺陷‘,
            ‘power‘: ‘0.2232‘,
          },
          {
            ‘name‘: ‘外部因素‘,
            ‘power‘: null,
          },
          {
            ‘name‘: ‘未查明降容‘,
            ‘power‘: null,
          },
          {
            ‘name‘: ‘计划停运‘,
            ‘power‘: null,
          },
          {
            ‘name‘: ‘设备故障‘,
            ‘power‘: null,
          },
          {
            ‘name‘: ‘限电‘,
            ‘power‘: ‘0.2232‘,
          },
        ],
        ‘ttt‘: null,
        ‘equivalentHours‘: ‘5.4028‘,
        ‘dayPlantUsePower‘: null,
        ‘lostPower‘: {
          ‘total‘: ‘0.8928‘,
          ‘lostList‘: [
            {
              ‘name‘: ‘光伏设备缺陷‘,
              ‘power‘: ‘0.2232‘,
            },
            {
              ‘name‘: ‘其他未查明‘,
              ‘power‘: null,
            },
            {
              ‘name‘: ‘厂外因素‘,
              ‘power‘: ‘0.2232‘,
            },
            {
              ‘name‘: ‘变电设备缺陷‘,
              ‘power‘: ‘0.2232‘,
            },
            {
              ‘name‘: ‘外部因素‘,
              ‘power‘: null,
            },
            {
              ‘name‘: ‘未查明降容‘,
              ‘power‘: null,
            },
            {
              ‘name‘: ‘计划停运‘,
              ‘power‘: null,
            },
            {
              ‘name‘: ‘设备故障‘,
              ‘power‘: null,
            },
            {
              ‘name‘: ‘限电‘,
              ‘power‘: ‘0.2232‘,
            },
          ],
        },
      },
    ];

  

var newArr = [];
    arr.map((item, index) => {
      newArr.push(...item.lostPower.lostList);
    });
    console.log(newArr, ‘dddd‘);
    const a = newArr.filter(one => !one.power);
    var map = {};
    //循环查找
    for (let i = 0; i < a.length; i++) {
      //数组里的i个元素
      var v = a[i].name;
      var v1 = a[i].power;
      //将数组的i个元素作为map对象的属性查看其属性值
      var counts = map[v];
      //如果map对象没有该属性,则设置该属性的值为1,有的话在其基础上再+1
      if (counts && map[v1] == null) {
        map[v] += 1;
      } else {
        map[v] = 1;
      }
    }
    console.log(map);
    var end = [];
    for (const k in map) {
      if (map[k] == arr.length) {
        end.push(k);
      }
    }
    console.log(end);
    const endArr = arr.map((item) => {
      const newItem = item.lostPower.lostList.map(one => {
        if (end.indexOf(one.name) > -1) {
          one = null;
        }
        return one;
      });
      return newItem;
    });
    console.log(endArr);
    var endIndex = [];
    endArr[0].map((item, index) => {
      if (!item) {
        endIndex.push(index);
      }
    });
    console.log(endIndex);

 

数组遍历对象的item去掉某一个item对象变成null

标签:nts   oss   com   rate   ide   tco   判断   put   去掉   

原文地址:https://www.cnblogs.com/xiaoyaoweb/p/13278468.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!