【问题标题】:Separating multiple objects from one programmatically以编程方式将多个对象与一个对象分离
【发布时间】: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


    【解决方案1】:

    通过匹配尾随数字,您可以将其放入适当的对象中。

    我假设 decription3 decription4 是一个错字。

    const tasks = {
        title1: "Quiet Time",
        title2: "Study",
        title3: "Go Jogging",
        title4: "Eat Breakfast",
        description1: "",
        description2: "",
        description3: "This is going to help to reach my goals and my life to the fullest",
        description4: "",
        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 tasksSorted = {};
    for (const [key, value] of Object.entries(tasks)) {
        const num = key.match(/\d+$/)[0];
        tasksSorted[num] ?? = {};
        tasksSorted[num][key] = value;
    }
    
    console.log(tasksSorted);

    【讨论】:

    • 惊人的答案,解决了!非常感谢!我需要阅读这部分内容以了解它的作用。 const num = key.match(/\d+$/)[0]; tasksSorted[num] ??= {};
    • 如果您投票并接受有用的答案,我会很好的@EthanNaidu
    • 绝对!哈哈,我需要等待 10 分钟才能点击解决,并且没有足够的声望来支持它。 @rustyBucketBay
    猜你喜欢
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 2010-11-14
    • 2011-12-24
    • 2017-12-18
    • 1970-01-01
    相关资源
    最近更新 更多