数组遍历对象的item去掉某一个item对象变成null
Posted 逍遥妹纸
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数组遍历对象的item去掉某一个item对象变成null相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章