英文:
splice objects from array and move those elements to a different element within array without mutating original
问题
这段代码大致实现了我需要的功能,但它改变了原始数组,这不是我想要的。我知道这是因为我在使用 splice
和 push
。
这个对象以每周的每一天作为键,每个键都有店铺的开放和关闭时间。
一切都正常,直到 friday
键,它只有 open
时间对象,而 close
时间在 saturday
键上(因为店铺在第二天关闭),saturday
和 sunday
也是同样的情况。
我想将每个对象移动到其相应的日期键,但我希望在不改变原始对象的情况下实现这一点。
我需要做什么才能实现这个目标?
英文:
This code does kinda what I need but it mutates the original array and that is not what I want. I know this happens because I'm using splice
and push
.
This object has every day of the week as key and each key has the opening and closing time of the store.
Everything is normal until the friday
key which has only the open
time object and the close
one is on the saturday
key(because the store closes the next day), same thing happens for saturday
and sunday
.
What I do is move each object to its corresponding day key but I want to achieve this without mutating the original object.
What am I missing to achieve that?
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const days = {
"monday": [{
"day": "monday",
"type": "open",
"value": 43200
},
{
"day": "monday",
"type": "close",
"value": 75600
}
],
"tuesday": [{
"day": "tuesday",
"type": "open",
"value": 43200
},
{
"day": "tuesday",
"type": "close",
"value": 75600
}
],
"wednesday": [{
"day": "wednesday",
"type": "open",
"value": 43200
},
{
"day": "wednesday",
"type": "close",
"value": 75600
}
],
"thursday": [{
"day": "thursday",
"type": "open",
"value": 43200
},
{
"day": "thursday",
"type": "close",
"value": 75600
}
],
"friday": [{
"day": "friday",
"type": "open",
"value": 36000
}],
"saturday": [{
"day": "friday",
"type": "close",
"value": 3600
},
{
"day": "saturday",
"type": "open",
"value": 36000
}
],
"sunday": [{
"day": "saturday",
"type": "close",
"value": 3600
},
{
"day": "sunday",
"type": "open",
"value": 43200
},
{
"day": "sunday",
"type": "close",
"value": 75600
}
]
}
const result = Object.entries(days).map(([k, v], i) => {
if (k == "friday") {
days[k].push(days.saturday.splice(0, 1));
//console.log(days[k])
} else if (k == "saturday") {
days[k].push(days.sunday.splice(0, 1));
//console.log(days[k])
}
return {
[k]: v
}
})
console.log(result)
<!-- end snippet -->
答案1
得分: 1
你可以取出所有内部对象并使用它们的 day
属性从头开始重建整个对象:
const days = {"monday": [{"day": "monday","type": "open","value": 43200},{"day": "monday","type": "close","value": 75600}],"tuesday": [{"day": "tuesday","type": "open","value": 43200},{"day": "tuesday","type": "close","value": 75600}],"wednesday": [{"day": "wednesday","type": "open","value": 43200},{"day": "wednesday","type": "close","value": 75600}],"thursday": [{"day": "thursday","type": "open","value": 43200},{"day": "thursday","type": "close","value": 75600}],"friday": [{"day": "friday","type": "open","value": 36000}],"saturday": [{"day": "friday","type": "close","value": 3600},{"day": "saturday","type": "open","value": 36000}],"sunday": [{"day": "saturday","type": "close","value": 3600},{"day": "sunday","type": "open","value": 43200},{"day": "sunday","type": "close","value": 75600}]};
const result = Object.values(days).flat().reduce((acc, o) => {
(acc[o.day] ??= []).push(o);
return acc;
}, {});
console.log(result);
希望对你有所帮助!
英文:
You could take all of the inner objects and use their day
properties to rebuild the overall object from scratch:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const days = {"monday": [{"day": "monday","type": "open","value": 43200},{"day": "monday","type": "close","value": 75600}],"tuesday": [{"day": "tuesday","type": "open","value": 43200},{"day": "tuesday","type": "close","value": 75600}],"wednesday": [{"day": "wednesday","type": "open","value": 43200},{"day": "wednesday","type": "close","value": 75600}],"thursday": [{"day": "thursday","type": "open","value": 43200},{"day": "thursday","type": "close","value": 75600}],"friday": [{"day": "friday","type": "open","value": 36000}],"saturday": [{"day": "friday","type": "close","value": 3600},{"day": "saturday","type": "open","value": 36000}],"sunday": [{"day": "saturday","type": "close","value": 3600},{"day": "sunday","type": "open","value": 43200},{"day": "sunday","type": "close","value": 75600}]};
const result = Object.values(days).flat().reduce((acc, o) => {
(acc[o.day] ??= []).push(o);
return acc;
}, {});
console.log(result);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论