简体   繁体   中英

Array of objects, filter out values with 0

I would like to remove the key + value of objects which have value of 0. the timestamp should stay in place.

 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) 

However my code doesn't remove 0 values

You could just delete the property with the value 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); 

Your keys.map is wrong:

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

Just use a simple filter statement

 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)); 

If you want to remove all keys which have a value of 0 you don't need to filter at all. Just iterate through the list and delete any properties with a value of 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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM