对象数组,用0过滤掉值

时间:2017-01-18 18:02:30

标签: javascript filter

我想删除值为0的key + value个对象。时间戳应该保留到位。

var array = [{
  "timestamp": "2017-01-18T00:00:00.000Z",
  "Test Bar 0": 78.09482851766388
}, {
  "timestamp": "2017-01-18T01:00:00.000Z",
  "Test Bar 0": 124.09589189108233
}, {
  "timestamp": "2017-01-18T02:00:00.000Z",
  "Test Bar 0": 106.97921714748477
}, {
  "timestamp": "2017-01-18T03:00:00.000Z",
  "Test Bar 0": 118.7469310337081
}, {
  "timestamp": "2017-01-18T04:00:00.000Z",
  "Test Bar 0": 119.81672320518294
}, {
  "timestamp": "2017-01-18T05:00:00.000Z",
  "Test Bar 0": 67.39690680291541
}, {
  "timestamp": "2017-01-18T06:00:00.000Z",
  "Test Bar 0": 117.67713886223325
}, {
  "timestamp": "2017-01-18T07:00:00.000Z",
  "Test Bar 0": 87.72295806093751
}, {
  "timestamp": "2017-01-18T08:00:00.000Z",
  "Test Bar 0": 115.53755451928356
}, {
  "timestamp": "2017-01-18T09:00:00.000Z",
  "Test Bar 0": 78.09482851766388
}, {
  "timestamp": "2017-01-18T10:00:00.000Z",
  "Test Bar 0": 48.14064771636815
}, {
  "timestamp": "2017-01-18T11:00:00.000Z",
  "Test Bar 0": 67.39690680291541
}, {
  "timestamp": "2017-01-18T12:00:00.000Z",
  "Test Bar 0": 130.5146449199314
}, {
  "timestamp": "2017-01-18T13:00:00.000Z",
  "Test Bar 0": 128.37506057698172
}, {
  "timestamp": "2017-01-18T14:00:00.000Z",
  "Test Bar 0": 73.81565983176449
}, {
  "timestamp": "2017-01-18T15:00:00.000Z",
  "Test Bar 0": 109.11880149043446
}, {
  "timestamp": "2017-01-18T16:00:00.000Z",
  "Test Bar 0": 127.30526840550688
}, {
  "timestamp": "2017-01-18T17:00:00.000Z",
  "Test Bar 0": 123.02609971960749
}, {
  "timestamp": "2017-01-18T18:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-18T19:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-18T20:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-18T21:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-18T22:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-18T23:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-19T00:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-19T01:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-19T02:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-19T03:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-19T04:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-19T05:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-19T06:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-19T07:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-19T08:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-19T09:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-19T10:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-19T11:00:00.000Z",
  "Test Bar 0": 0
}]


var abc = array.filter((object) => {
  let keys = []
  for (let key in object) {
    if (key !== 'timestamp') keys.push(key)
  }
  return keys.map(key => object[key] > 0)
})

console.log(abc)

但是我的代码不会删除0值

4 个答案:

答案 0 :(得分:1)

您只需删除值为0的属性。

var array = [{ timestamp: "2017-01-18T00:00:00.000Z", "Test Bar 0": 78.09482851766388 }, { timestamp: "2017-01-18T01:00:00.000Z", "Test Bar 0": 124.09589189108233 }, { timestamp: "2017-01-18T02:00:00.000Z", "Test Bar 0": 106.97921714748477 }, { timestamp: "2017-01-18T03:00:00.000Z", "Test Bar 0": 118.7469310337081 }, { timestamp: "2017-01-18T04:00:00.000Z", "Test Bar 0": 119.81672320518294 }, { timestamp: "2017-01-18T05:00:00.000Z", "Test Bar 0": 67.39690680291541 }, { timestamp: "2017-01-18T06:00:00.000Z", "Test Bar 0": 117.67713886223325 }, { timestamp: "2017-01-18T07:00:00.000Z", "Test Bar 0": 87.72295806093751 }, { timestamp: "2017-01-18T08:00:00.000Z", "Test Bar 0": 115.53755451928356 }, { timestamp: "2017-01-18T09:00:00.000Z", "Test Bar 0": 78.09482851766388 }, { timestamp: "2017-01-18T10:00:00.000Z", "Test Bar 0": 48.14064771636815 }, { timestamp: "2017-01-18T11:00:00.000Z", "Test Bar 0": 67.39690680291541 }, { timestamp: "2017-01-18T12:00:00.000Z", "Test Bar 0": 130.5146449199314 }, { timestamp: "2017-01-18T13:00:00.000Z", "Test Bar 0": 128.37506057698172 }, { timestamp: "2017-01-18T14:00:00.000Z", "Test Bar 0": 73.81565983176449 }, { timestamp: "2017-01-18T15:00:00.000Z", "Test Bar 0": 109.11880149043446 }, { timestamp: "2017-01-18T16:00:00.000Z", "Test Bar 0": 127.30526840550688 }, { timestamp: "2017-01-18T17:00:00.000Z", "Test Bar 0": 123.02609971960748 }, { timestamp: "2017-01-18T18:00:00.000Z", "Test Bar 0": 0 }, { timestamp: "2017-01-18T19:00:00.000Z", "Test Bar 0": 0 }, { timestamp: "2017-01-18T20:00:00.000Z", "Test Bar 0": 0 }, { timestamp: "2017-01-18T21:00:00.000Z", "Test Bar 0": 0 }, { timestamp: "2017-01-18T22:00:00.000Z", "Test Bar 0": 0 }, { timestamp: "2017-01-18T23:00:00.000Z", "Test Bar 0": 0 }, { timestamp: "2017-01-19T00:00:00.000Z", "Test Bar 0": 0 }, { timestamp: "2017-01-19T01:00:00.000Z", "Test Bar 0": 0 }, { timestamp: "2017-01-19T02:00:00.000Z", "Test Bar 0": 0 }, { timestamp: "2017-01-19T03:00:00.000Z", "Test Bar 0": 0 }, { timestamp: "2017-01-19T04:00:00.000Z", "Test Bar 0": 0 }, { timestamp: "2017-01-19T05:00:00.000Z", "Test Bar 0": 0 }, { timestamp: "2017-01-19T06:00:00.000Z", "Test Bar 0": 0 }, { timestamp: "2017-01-19T07:00:00.000Z", "Test Bar 0": 0 }, { timestamp: "2017-01-19T08:00:00.000Z", "Test Bar 0": 0 }, { timestamp: "2017-01-19T09:00:00.000Z", "Test Bar 0": 0 }, { timestamp: "2017-01-19T10:00:00.000Z", "Test Bar 0": 0 }, { timestamp: "2017-01-19T11:00:00.000Z", "Test Bar 0": 0 }];

array.forEach(o => Object.keys(o).forEach(k => {
    if (k !== 'timestamp' && o[k] === 0) {
        delete o[k];
    }
}));

console.log(array);

答案 1 :(得分:1)

你的keys.map错了:

var abc = array.forEach((object) => {
  for(key in object){
    if(key!=="timestamp"&&(object[key]==0||object[key]=="0") {
        delete object[key];
     }
    }
 });

答案 2 :(得分:1)

只需使用简单的filter声明

即可



var array = [{"timestamp":"2017-01-18T00:00:00.000Z","Test Bar 0":78.09482851766388},{"timestamp":"2017-01-18T01:00:00.000Z","Test Bar 0":124.09589189108233},{"timestamp":"2017-01-18T02:00:00.000Z","Test Bar 0":106.97921714748477},{"timestamp":"2017-01-18T03:00:00.000Z","Test Bar 0":118.7469310337081},{"timestamp":"2017-01-18T04:00:00.000Z","Test Bar 0":119.81672320518294},{"timestamp":"2017-01-18T05:00:00.000Z","Test Bar 0":67.39690680291541},{"timestamp":"2017-01-18T06:00:00.000Z","Test Bar 0":117.67713886223325},{"timestamp":"2017-01-18T07:00:00.000Z","Test Bar 0":87.72295806093751},{"timestamp":"2017-01-18T08:00:00.000Z","Test Bar 0":115.53755451928356},{"timestamp":"2017-01-18T09:00:00.000Z","Test Bar 0":78.09482851766388},{"timestamp":"2017-01-18T10:00:00.000Z","Test Bar 0":48.14064771636815},{"timestamp":"2017-01-18T11:00:00.000Z","Test Bar 0":67.39690680291541},{"timestamp":"2017-01-18T12:00:00.000Z","Test Bar 0":130.5146449199314},{"timestamp":"2017-01-18T13:00:00.000Z","Test Bar 0":128.37506057698172},{"timestamp":"2017-01-18T14:00:00.000Z","Test Bar 0":73.81565983176449},{"timestamp":"2017-01-18T15:00:00.000Z","Test Bar 0":109.11880149043446},{"timestamp":"2017-01-18T16:00:00.000Z","Test Bar 0":127.30526840550688},{"timestamp":"2017-01-18T17:00:00.000Z","Test Bar 0":123.02609971960749},{"timestamp":"2017-01-18T18:00:00.000Z","Test Bar 0":0},{"timestamp":"2017-01-18T19:00:00.000Z","Test Bar 0":0},{"timestamp":"2017-01-18T20:00:00.000Z","Test Bar 0":0},{"timestamp":"2017-01-18T21:00:00.000Z","Test Bar 0":0},{"timestamp":"2017-01-18T22:00:00.000Z","Test Bar 0":0},{"timestamp":"2017-01-18T23:00:00.000Z","Test Bar 0":0},{"timestamp":"2017-01-19T00:00:00.000Z","Test Bar 0":0},{"timestamp":"2017-01-19T01:00:00.000Z","Test Bar 0":0},{"timestamp":"2017-01-19T02:00:00.000Z","Test Bar 0":0},{"timestamp":"2017-01-19T03:00:00.000Z","Test Bar 0":0},{"timestamp":"2017-01-19T04:00:00.000Z","Test Bar 0":0},{"timestamp":"2017-01-19T05:00:00.000Z","Test Bar 0":0},{"timestamp":"2017-01-19T06:00:00.000Z","Test Bar 0":0},{"timestamp":"2017-01-19T07:00:00.000Z","Test Bar 0":0},{"timestamp":"2017-01-19T08:00:00.000Z","Test Bar 0":0},{"timestamp":"2017-01-19T09:00:00.000Z","Test Bar 0":0},{"timestamp":"2017-01-19T10:00:00.000Z","Test Bar 0":0},{"timestamp":"2017-01-19T11:00:00.000Z","Test Bar 0":0}];

console.log(array.filter(o => o["Test Bar 0"] > 0));




答案 3 :(得分:1)

如果您要删除价值为0的所有密钥,则根本不需要进行过滤。只需遍历列表并删除值为0的任何属性。



var array = [{
  "timestamp": "2017-01-18T00:00:00.000Z",
  "Test Bar 0": 78.09482851766388
}, {
  "timestamp": "2017-01-18T01:00:00.000Z",
  "Test Bar 0": 124.09589189108233
}, {
  "timestamp": "2017-01-18T02:00:00.000Z",
  "Test Bar 0": 106.97921714748477
}, {
  "timestamp": "2017-01-18T03:00:00.000Z",
  "Test Bar 0": 118.7469310337081
}, {
  "timestamp": "2017-01-18T04:00:00.000Z",
  "Test Bar 0": 119.81672320518294
}, {
  "timestamp": "2017-01-18T05:00:00.000Z",
  "Test Bar 0": 67.39690680291541
}, {
  "timestamp": "2017-01-18T06:00:00.000Z",
  "Test Bar 0": 117.67713886223325
}, {
  "timestamp": "2017-01-18T07:00:00.000Z",
  "Test Bar 0": 87.72295806093751
}, {
  "timestamp": "2017-01-18T08:00:00.000Z",
  "Test Bar 0": 115.53755451928356
}, {
  "timestamp": "2017-01-18T09:00:00.000Z",
  "Test Bar 0": 78.09482851766388
}, {
  "timestamp": "2017-01-18T10:00:00.000Z",
  "Test Bar 0": 48.14064771636815
}, {
  "timestamp": "2017-01-18T11:00:00.000Z",
  "Test Bar 0": 67.39690680291541
}, {
  "timestamp": "2017-01-18T12:00:00.000Z",
  "Test Bar 0": 130.5146449199314
}, {
  "timestamp": "2017-01-18T13:00:00.000Z",
  "Test Bar 0": 128.37506057698172
}, {
  "timestamp": "2017-01-18T14:00:00.000Z",
  "Test Bar 0": 73.81565983176449
}, {
  "timestamp": "2017-01-18T15:00:00.000Z",
  "Test Bar 0": 109.11880149043446
}, {
  "timestamp": "2017-01-18T16:00:00.000Z",
  "Test Bar 0": 127.30526840550688
}, {
  "timestamp": "2017-01-18T17:00:00.000Z",
  "Test Bar 0": 123.02609971960749
}, {
  "timestamp": "2017-01-18T18:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-18T19:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-18T20:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-18T21:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-18T22:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-18T23:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-19T00:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-19T01:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-19T02:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-19T03:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-19T04:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-19T05:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-19T06:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-19T07:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-19T08:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-19T09:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-19T10:00:00.000Z",
  "Test Bar 0": 0
}, {
  "timestamp": "2017-01-19T11:00:00.000Z",
  "Test Bar 0": 0
}];

array.forEach((object) => {
  Object.keys(object).forEach((key) => {
    if (object[key] === 0) {
      delete object[key];
    }
  });
});

console.log(array);