2

I have an historical object with values for each date in a range:

response = {
          2019-09-13: {logged-in: 3, clicked-something: 15}
          2019-09-14: {logged-in: 0, clicked-something: 0}
          2019-09-15: {logged-in: 0, clicked-something: 0}
          2019-09-16: {logged-in: 0, clicked-something: 0}
          2019-09-17: {logged-in: 0, clicked-something: 0}
          2019-09-18: {logged-in: 2, clicked-something: 24}
          2019-09-19: {logged-in: 7, clicked-something: 36}
          }

I only need one of the properties so trying to filter the object into this:

          2019-09-13: {clicked-something: 15}
          2019-09-14: {clicked-something: 0}
          2019-09-15: {clicked-something: 0}
          2019-09-16: {clicked-something: 0}
          2019-09-17: {clicked-something: 0}
          2019-09-18: {clicked-something: 24}
          2019-09-19: {clicked-something: 36}
          }

I have tried to give following method to solve this:

const data = {
  "2019-09-13": { "logged-in": 3, "clicked-something": 15 },
  "2019-09-14": { "logged-in": 0, "clicked-something": 0 },
  "2019-09-15": { "logged-in": 0, "clicked-something": 0 },
  "2019-09-16": { "logged-in": 0, "clicked-something": 0 },
  "2019-09-17": { "logged-in": 0, "clicked-something": 0 },
  "2019-09-18": { "logged-in": 2, "clicked-something": 24 },
  "2019-09-19": { "logged-in": 7, "clicked-something": 36 }
};

const allowed = ["clicked-something"]

const result = Object.values(data).map(item => {
  return Object.keys(item)
    .filter(key => allowed.includes(key))
    .reduce((obj, key) => {
      obj[key] = item[key]
      return obj
    }, {})
})

console.log(result)

it does the job but gives me an array

edit: I have constructed my solution by using JavaScript: filter() for Objects. Problem here is changing date keys of the response object. Also in some occasions I may need to filter 3 out of 5, etc.

Taki
  • 17,320
  • 4
  • 26
  • 47
Naim Mustafa
  • 314
  • 2
  • 14
  • 1
    Possible duplicate of [JavaScript: filter() for Objects](https://stackoverflow.com/questions/5072136/javascript-filter-for-objects) – Michelangelo Sep 19 '19 at 15:03
  • I have actually saw that post and construct my solution by using it. which works but I am returning to an array instead of an object and also date keys are always changing by time goes. – Naim Mustafa Sep 19 '19 at 15:05

1 Answers1

3

.reduce the Object.entries

const response = {
  "2019-09-13": { "logged-in": 3, "clicked-something": 15 },
  "2019-09-14": { "logged-in": 0, "clicked-something": 0 },
  "2019-09-15": { "logged-in": 0, "clicked-something": 0 },
  "2019-09-16": { "logged-in": 0, "clicked-something": 0 },
  "2019-09-17": { "logged-in": 0, "clicked-something": 0 },
  "2019-09-18": { "logged-in": 2, "clicked-something": 24 },
  "2019-09-19": { "logged-in": 7, "clicked-something": 36 }
};

const allowed = ["clicked-something"];

const result = Object.entries(response).reduce((acc, [key, value]) => {
  acc[key] = allowed.reduce((ac, k) => {
    ac[k] = value[k];
    return ac;
  }, {});
  return acc;
}, {});

console.log(result);
Taki
  • 17,320
  • 4
  • 26
  • 47
  • 1
    Thank you!, i cant believe how I missed it, I am mapping it instead of reducing, duh – Naim Mustafa Sep 19 '19 at 15:11
  • you could do it your way aswell. include const allowed = ["clicked-something", "clicked-something-else"] –  Sep 19 '19 at 15:19