Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

162
Visualizações
update nested value in object of objects with keys in javascript

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?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

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;
});
about 4 years ago · Juan Pablo Isaza Relatório

0

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

about 4 years ago · Juan Pablo Isaza Relatório

0

for (let key in obj) {
  obj[key].created_at = obj[key].created_at.seconds;
  obj[key].updated_at = obj[key].updated_at.seconds;
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda