1

I have a nested object:

myObject {
  "fshkj78gds": {
    "name": "Joe",
    "created_at": {
      "nanoseconds": 745000000,
      "seconds": 1645468219,
    },
    "updated_at": {
      "nanoseconds": 0,
      "seconds": 1645471800,
    },
  },
  "gsdg987": {
    "name": "Mike",
    "created_at": {
      "nanoseconds": 745000000,
      "seconds": 1645468219,
    },
    "updated_at": {
      "nanoseconds": 0,
      "seconds": 1645471800,
    },
  },
}

I would like to convert created_at and updated_at in each nested object to only return the seconds instead as a number like so:

myObject {
  "fshkj78gds": {
    "name": "Joe",
    "created_at": 1645468219,
    "updated_at": 1645471800,
  },
  "gsdg987": {
    "name": "Joe",
    "created_at": 1645468219,
    "updated_at": 1645471800,
  },
}

I am thinking I will need a mix of map and Object.assign as well as spread operator?

So far I got to:

Object.assign({}, myObject, Object.values(myObject).map((nestedObject) =>
  Object.assign({}, nestedObject, {...nestedObject, created_at: nestedObject.created_at.seconds})
))

But this generates an array of objects and doesn't keep the original structure. What's the proper way to do this?

4
  • Can you modify the object in place, or do you need to return a new object? Commented Feb 21, 2022 at 21:04
  • modifying the original is fine Commented Feb 21, 2022 at 21:05
  • To do it in place (modifying the original array and its original objects), use a for loop, adding a field Commented Feb 21, 2022 at 21:06
  • These are all great solutions, so it's hard to select which one is the right answer. I'm choosing @Barmar because it's the one I chose to use since I can modify the original object and in my use case ended up being the least verbose. Thank you all! Commented Feb 21, 2022 at 21:23

3 Answers 3

3

Use forEach() to iterate over the values and then just reassign the properties.

Object.values(myObject).forEach(item => {
    item.created_at = item.created_at.seconds;
    item.updated_at = item.updated_at.seconds;
});
Sign up to request clarification or add additional context in comments.

Comments

2

Using Object#fromEntries, Object#entries, and Array#map:

const myObject = {
  "fshkj78gds": {
    "name": "Joe",
    "created_at": { "nanoseconds": 745000000, "seconds": 1645468219 },
    "updated_at": { "nanoseconds": 0, "seconds": 1645471800 }
  },
  "gsdg987": {
    "name": "Mike",
    "created_at": { "nanoseconds": 745000000, "seconds": 1645468219 },
    "updated_at": { "nanoseconds": 0, "seconds": 1645471800 }
  }
};

const res = Object.fromEntries(
  Object.entries(myObject).map(([ key, value ]) => ([
    key,
    { ...value, 'created_at': value['created_at'].seconds, 'updated_at': value['updated_at'].seconds,  }
  ]))
);

console.log(res);

1 Comment

Very cool! This directly demonstrate all I asked about.
1
for (let key in obj) {
  obj[key].created_at = obj[key].created_at.seconds;
  obj[key].updated_at = obj[key].updated_at.seconds;
}

Comments

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.