如何使用 es6 在函数中将 object 解构为新的 object

How do I destructure an object into new objects in a function using es6

代码如下:

const toDoList = {
    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 { title1, description1, date1, time1, completed1, priority1, tags1 } = toDoList;

我还没弄清楚如何通过这一点,我不确定上面的行是否会在函数中使用。 非常感激! 编辑: 我试图解决的挑战是这个 post 的原因: 您面临的挑战是解构单个 object 形式的数据,然后将数据重组为多个单独的 object,每个代表一个任务数据。 这必须全部以编程方式完成。 作为额外的挑战,请考虑如何使用参数来配置函数的行为,以便您可以代表 1 或 100(或任意数量)用户,而无需重写任何函数定义。 编辑 2: 我相信我的最终结果应该是 4 objects,每个都有自己的标题、描述、日期等。 类似于它的工作方式:const { title1, description1, date1, time1, completed1, priority1, tags1 } = toDoList; 然而,这不会是干编码,因为我必须拥有它,这样我就可以 运行 通过代码,即使有大约 100 个标题每个都需要它自己的 object.

使用Object.entries and reduce 使用数组作为累加器

我使用正则表达式将密钥分解为 keyName 和 IDX

const toDoList = { 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 objects = Object.entries(toDoList).reduce((acc,[key,value]) => {
   // match returns match, group1 (key) and group2 (idx)
   const [_,keyName,idx] = key.match(/([^\d]+)(\d)/); // getting the key and idx
   if (acc.length<+idx) {
     acc.push({[keyName]:value})
   }  
   else acc[idx-1][keyName] = value
   return acc
 },[])
 console.log(objects)

或者这个,使用对象作为累加器。

const toDoList = { 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 objects = Object.entries(toDoList).reduce((acc,[key,value]) => {
   const keyName = key.replace(/\d/g,"")
   acc[keyName] = acc[keyName] || []
   acc[keyName].push(value)
   return acc
 },{})
 console.log(objects)

我认为它不会为您提供完整的解决方案,但这里是从 toDoList

中提取任务的一种方法的示例
const task1 = {};
for (const [key, value] of Object.entries(toDoList)) {
  if (key.endsWith('1')) {
    task1[key] = Array.isArray(value) ? [...value] : value;
  }
}

console.dir(task1);

/*
{ 
  completed1: false
  date1: "05/02/2020"
​  description1: ""
​  priority1: "red"
​  tags1: [ "Personal", "Work", "School" ]
​  time1: "08:12"
​  title1: "Quiet Time"
}
*/

此答案与@mplungjan 相似。

const toDoList = { 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"] };

let deconstructed_objects = Object.entries(toDoList).reduce((a, b) => {
    // Splits the property name and the number index (returns an array)
    const [property, index] = b[0].match(/\D+|\d+/gi);
    
    // Assign values to an object based on the number index - 1
    a[parseInt(index)-1] = {...a[parseInt(index)-1], [property]: b[1]}
    return a 
}, []);

console.log(deconstructed_objects);