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

223
Visualizações
map on the lower level key of a nested object

I had posted this question earlier but someone deleted with How can I access and process nested objects, arrays or JSON? as possible answer. This is not helping me since a) The key 'date' is spread across several names, b)The objects comprises on arrays & objects & c) The depth at which the key 'date' is present can change.

Hence posting the question again.

I have a JS object as below

const bb = {
  Harry: {
    details: [
      {
        cat: "Life",
        values: [
          {
            date: "2021-08-02",
            level: 77.6,
          },
          {
            date: "2021-08-03",
            level: 79.1,
          },
        ],
      },
    ],
  },
  Barry: {
    details: [
      {
        cat: "Logic",
        values: [
          {
            date: "2021-08-02",
            level: 77.6,
          },
          {
            date: "2021-08-03",
            level: 79.1,
          },
        ],
      },
    ],
  },
};

I also have a variable defined for parsing the dates const dateParse = d3.timeParse("%Y-%m-%d")

I want to parse all the dates in the object. Since the 'date' is few levels below in the object, I am not able to figure this out. How do I go about it?

The expected output is

const bb = {
  Harry: {
    details: [
      {
        cat: "Life",
        values: [
          {
            date: Mon Aug 02 2021 00:00:00 GMT+0530 (India Standard Time),
            level: 77.6,
          },
          {
            date: Tue Aug 03 2021 00:00:00 GMT+0530 (India Standard Time),
            level: 79.1,
          },
        ],
      },
    ],
  },
  Barry: {
    details: [
      {
        cat: "Logic",
        values: [
          {
            date: Mon Aug 02 2021 00:00:00 GMT+0530 (India Standard Time),
            level: 77.6,
          },
          {
            date: Tue Aug 03 2021 00:00:00 GMT+0530 (India Standard Time),
            level: 79.1,
          },
        ],
      },
    ],
  },
};
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You just have to loop through the objects, select the date node and execute

(new Date(datestring)).toString()

This will generate the date as specified in your output.

const bb = { Harry: { details: [{cat: "Life",values: [{date: "2021-08-02",level: 77.6},{date: "2021-08-03",level: 79.1}]}]},Barry: {details: [{cat: "Logic",values: [{date: "2021-08-02",level: 77.6},{date: "2021-08-03",level: 79.1}]}]}};
Object.values(bb).forEach((value) => {
  value.details.forEach((detail) => {
    detail.values.forEach((value) => {
      value.date = (new Date(value.date)).toString();
    })
  })
});
console.log(bb);

about 4 years ago · Juan Pablo Isaza Relatório

0

If you want to parse all the nested date keys, you can do it recursively using the below functions

const bb = { Harry: { details: [{cat: "Life",values: [{date: "2021-08-02",level: 77.6},{date: "2021-08-03",level: 79.1}]}]},Barry: {details: [{cat: "Logic",values: [{date: "2021-08-02",level: 77.6},{date: "2021-08-03",level: 79.1}]}]}};

function traverseArray(inputArray) {
  for (const arrayValue of inputArray) {
    traverseObject(arrayValue);
  }
}

function traverseObject(inputObject) {
  for (const key in inputObject) {
    const value = inputObject[key];
    const valueType = typeof(value);
    if (valueType == "object") {
      traverseObject(value);
    } else if (valueType == "array") {
      traverseArray(value);
    } else if (key == "date") { // since I want to parse only date keys
      inputObject[key] = 'd3.timeParse("%Y-%m-%d") - Parse date here';
    }
  }
}
        
traverseObject(bb);
console.log(bb);
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