1

I have an object of objects and I want to change the keys of the objects

"opening_hours": {
  "0": {
    "close": "17:00:00",
    "is_open": true,
    "open": "09:00:00"
  },
  "1": {
    "close": "17:00:00",
    "is_open": true,
    "open": "09:00:00"
  },
  "2": {
    "close": "17:00:00",
    "is_open": true,
    "open": "09:00:00"
  },
  "3": {
    "close": "17:00:00",
    "is_open": true,
    "open": "09:00:00"
  },
  "4": {
    "close": "17:00:00",
    "is_open": true,
    "open": "09:00:00"
  },
  "5": {
    "is_open": false
  },
  "6": {
    "is_open": false
  }
}

like:"0" I want it to be 'Monday', Is this possible in Javascript?

Expected Output is :

{
  "monday": {
    "close": "17:00:00",
    "is_open": true,
    "open": "09:00:00"
  },
  "tuesday": {
    "close": "17:00:00",
    "is_open": true,
    "open": "09:00:00"
  },
  "wednesday": {
    "close": "17:00:00",
    "is_open": true,
    "open": "09:00:00"
  },
  "thursday": {
    "close": "17:00:00",
    "is_open": true,
    "open": "09:00:00"
  },
  "friday": {
    "close": "17:00:00",
    "is_open": true,
    "open": "09:00:00"
  },
  "saturday": {
    "is_open": false
  },
  "sunday": {
    "is_open": false
  }
}
2
  • Hello @rasika your question has been marked Flag by moderator. If you need solution you can contact me ! Commented Jul 20, 2019 at 3:54
  • 1
    @Rishab What do you mean by "marked Flag by moderator"? Commented Jul 20, 2019 at 4:00

1 Answer 1

3

Create a days array which has index of an item same as the day (Eg: 0 for monday and so on). Loop through the keys of opening_hours object and add new key-value pairs to the output like this:

const days = ["monday","tuesday","wednesday","thursday","friday","saturday","sunday"],
    opening_hours = {"0":{close:"17:00:00",is_open:true,open:"09:00:00"},"1":{close:"17:00:00",is_open:true,open:"09:00:00"},"2":{close:"17:00:00",is_open:true,open:"09:00:00"},"3":{close:"17:00:00",is_open:true,open:"09:00:00"},"4":{close:"17:00:00",is_open:true,open:"09:00:00"},"5":{is_open:false},"6":{is_open:false}},
    output = {};

for(const key in opening_hours)
  output[days[key]] = opening_hours[key];
  
console.log(output)

You could also map the entries of the object and update the number to days[number] to get the string. Then use Object.fromEntries() to create a new object.

const days = ["monday","tuesday","wednesday","thursday","friday","saturday","sunday"],
    opening_hours={"0":{close:"17:00:00",is_open:true,open:"09:00:00"},"1":{close:"17:00:00",is_open:true,open:"09:00:00"},"2":{close:"17:00:00",is_open:true,open:"09:00:00"},"3":{close:"17:00:00",is_open:true,open:"09:00:00"},"4":{close:"17:00:00",is_open:true,open:"09:00:00"},"5":{is_open:false},"6":{is_open:false}},
    updatedEntries = Object.entries(opening_hours).map(([k, v]) => [days[k], v]),
    output = Object.fromEntries(updatedEntries);

console.log(output)

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.