1

The data format, which I get from back-end is

{
  "2020-08-22": 1425,
  "2020-08-23": 1475,
  "2020-08-24": 1475,
  "flightnumber": "EK-853",
  "source": "emirates",
  "stops": "stops-0"
}

Is that possible to change the key data as following

{
  "Aug 22": 1425,
  "Aug 23": 1475,
  "Aug 24": 1475,
  "flightnumber": "EK-853",
  "source": "emirates",
  "stops": "stops-0"
}

Please tell me some solution for this.

1 Answer 1

1

Below snippet could help you. It has several things to notice:

  • iterate through object's keys
  • object's dynamic key
  • detect invalid date

const data = {
  "2020-08-22": 1425,
  "2020-08-23": 1475,
  "2020-08-24": 1475,
  flightnumber: "EK-853",
  source: "emirates",
  stops: "stops-0",
}

const monthNames = [
  "Jan",
  "Feb",
  "Mar",
  "Apr",
  "May",
  "Jun",
  "Jul",
  "Aug",
  "Sep",
  "Oct",
  "Nov",
  "Dec",
]

const res = {}

Object.keys(data).forEach((k) => {
  const date = new Date(k)

  if (!isNaN(date.getTime())) {
    res[`${monthNames[date.getMonth()]} ${date.getDate()}`] = data[k]
  } else {
    res[k] = data[k]
  }
})

console.log(res)

Sign up to request clarification or add additional context in comments.

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.