Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

222
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!