1

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

9
  • 2
    I don't see any code in here that attempts to filter out zeros. Doesn't Javascript have a filter function? Commented Jan 18, 2017 at 18:04
  • Are you saying you want to remove any properties that have a 0 value? Or that you want to get all timestamps associated with non-zero values? Commented Jan 18, 2017 at 18:05
  • the keys of the objects are unkown. I did filter out all objects, stored the keys which are not timestamp and returned only > 0 Commented Jan 18, 2017 at 18:05
  • the timestamp + the [key] [value] of keys which are greater then 0 Commented Jan 18, 2017 at 18:06
  • 1
    @JasonBrill map doesn't filter. You're just creating a list of boolean values with that map by the looks of it. I think you meant filter. Commented Jan 18, 2017 at 18:06

4 Answers 4

1

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];
     }
    }
 });
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

Comments

1

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

1 Comment

Beautiful! Thank you! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.