简体   繁体   English

在javascript中修改对象的键

[英]Modify keys of object in javascript

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? 喜欢:“0”我希望它是'星期一',这可能在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
  }
}

Create a days array which has index of an item same as the day (Eg: 0 for monday and so on). 创建一个具有与日期相同的项目索引的days数组(例如: monday0 ,依此类推)。 Loop through the keys of opening_hours object and add new key-value pairs to the output like this: 循环遍历opening_hours对象的键,并将新的键值对添加到输出中,如下所示:

 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. 您还可以map对象的entries ,并将数字更新为days[number]以获取字符串。 Then use Object.fromEntries() to create a new object. 然后使用Object.fromEntries()创建一个新对象。

 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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM