【发布时间】:2021-09-01 21:30:21
【问题描述】:
我有一个包含多个任务的对象。挑战是将它们分类为单独的对象,我已经设法使用下面的代码来完成。我想知道如何将其调整为循环将对任务进行排序而无需单独编程的状态(就像 i++ 类型的函数)。任何想法如何做到这一点?
const tasks = {
title1: "Quiet Time",
title2: "Study",
title3: "Go Jogging",
title4: "Eat Breakfast",
description1: "",
description2: "",
decription3: "This is going to help to reach my goals and my life to the fullest",
decription4: "",
date1: "05/02/2020",
date2: "01/02/2020",
date3: "tomorrow",
date4: "today",
time1: "08:12",
time2: "13:15",
time3: "12:36",
time4: "13:25",
completed1: false,
completed2: true,
completed3: false,
completed4: true,
priority1: "red",
priority2: "yellow",
priority3: "black",
priority4: "white",
tags1: ["Personal", "Work", "School"],
tags2: ["Personal", "School", "Diary Entry"],
tags3: ["Content Creation", "Personal"],
tags4: ["Personal"]
}
const task1 = {};
const task2 = {};
const task3 = {};
const task4 = {};
for (let [key, value] of Object.entries(tasks)) {
if (key.endsWith('1')) {
task1[key] = value;
}
if (key.endsWith('2')) {
task2[key] = value;
}
if (key.endsWith('3')) {
task3[key] = value;
}
if (key.endsWith('4')) {
task4[key] = value;
}
}
【问题讨论】:
标签: javascript loops javascript-objects