gpt4 book ai didi

javascript - 使用 JavaScript 以高效的方式将对象数组中的 JSON 对象添加到另一个复杂的对象数组中

转载 作者:行者123 更新时间:2023-11-29 20:55:15 24 4
gpt4 key购买 nike

输入:

[
{
"date": "2018-03-31T15:00:00.000Z",
"value": "Some Value"
},
{
"date": "2018-04-01T15:00:00.000Z",
"value": "Some Value"
},
{
"date": "2018-04-02T15:00:00.000Z",
"value": "Some Value"
},
so on...
]

输入完整的 JSON:https://api.myjson.com/bins/1e5qmn

输出:

[
{
"week": {
"days": [
{
"date": 1,
"day": "Sun",
"formattedDate": "04/01(Sun)"
},
{
"date": 2,
"day": "Mon",
"formattedDate": "04/02(Mon)"
},
so on...
]
}
},
{
"week": {

}
},
{
"week": {

}
},
so on...
]

输出完整的 JSON:https://api.myjson.com/bins/d8v4f

我可以使用以下代码实现输出。我正在使用 momentjs 来格式化日期(这很好):

 var daysArray = [];
for (var i = 0; i < results.length; i++) {
var dayOb = {
date: '',
day: '',
formattedDate: '',
};

var date = results[i].date;

dayOb.date = moment(date).date();
dayOb.day = moment(date).format('ddd');
dayOb.formattedDate = moment(date).format('MM/DD(ddd)');

daysArray.push(dayOb);
}

var weeksArray = [];
var count = 0;
for (var i = 0; i < Math.ceil(daysArray.length / 7); i++) {
var weekOb = {
week: {
days: [],
},
};
weeksArray.push(weekOb);
}

var nextStart = 0;
for (var i = 0; i < weeksArray.length; i++) {
var count = 0;
for (var j = nextStart; j < daysArray.length; j++) {
count++;
if (count < 8) {
weeksArray[i].week['days'].push(daysArray[j]);
} else {
nextStart = j;
break;
}
}
count = 0;
}

工作 fiddle 链接:https://jsfiddle.net/r2og6c54/

问题:是否有更有效的方法来实现这一点,即 JavaScript native 方法或减少这些太多的 for 循环等?欢迎提出任何建议。谢谢!

最佳答案

这是一个在 vanilla JavaScript 中的解决方案,没有使用 moment.js:

const input = [{
"date": "2018-03-31T15:00:00.000Z",
"value": "Some Value"
},
{
"date": "2018-04-01T15:00:00.000Z",
"value": "Some Value"
},
{
"date": "2018-04-02T15:00:00.000Z",
"value": "Some Value"
}
];
const output = Array(Math.ceil(input.length / 7))
.fill()
.map((_, i) => i * 7)
.map(i => input.slice(i, i + 7))
.map(w => ({
week: {
days: w.map(d => new Date(d.date)).map(d => {
const date = d.toLocaleDateString('en', {day: '2-digit'});
const month = d.toLocaleDateString('en', {month: '2-digit'});
const day = d.toLocaleDateString('en', {weekday: 'short'})
return {date, day, formattedDate: `${month}/${date}(${day})`};
})
}
}));

console.log(output)

如果您还想处理,您可以按如下方式进行:

const output = Array(Math.ceil(input.length / 7))
.fill()
.map((_, i) => i * 7)
.map(i => input.slice(i, i + 7))
.map(w => ({
week: {
days: w.map(d => {
const value = d.value;
const date = new Date(d.date);
const day = date.toLocaleDateString('en', {day: '2-digit'});
const month = date.toLocaleDateString('en', {month: '2-digit'});
const weekday = date.toLocaleDateString('en', {weekday: 'short'})
return {day, weekday, formattedDate: `${month}/${day}(${weekday})`, value};
})
}
}));

关于javascript - 使用 JavaScript 以高效的方式将对象数组中的 JSON 对象添加到另一个复杂的对象数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49607325/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com