1

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,
          },
        ],
      },
    ],
  },
};
moys
  • 7,747
  • 2
  • 11
  • 42
  • There should be a way to parse the date before the chart is rendered in the D3 itself. This will save extra iterations on your input object – ihimv Sep 22 '21 at 04:22
  • @ihimv D3 indeed has that option but I am not able to 'locate' the date key (hence the question) – moys Sep 22 '21 at 04:31

2 Answers2

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);
Nitheesh
  • 19,238
  • 3
  • 22
  • 49
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);
ihimv
  • 1,308
  • 12
  • 25