ES6还原为新对象

时间:2019-02-10 12:22:00

标签: javascript reactjs ecmascript-6

我一年中的所有日子都是这样的:

PATH

我有一个对象可以保存特定日期的计划任务和完成任务:

const days = ["2019-01-01", "2019-01-02", "2019-01-03" ...]

我想要的是一个新对象,该对象包含全天的信息(无论是否计划和完成任务),像这样:

const tasks = {"2019-01-01": {"planned": 3, "completed": 2,}, "2019-01-03": { "planned": 1, "completed": 0 }, "2019-01-10": { "planned": 1, "completed": 1} ... }

我知道这在某种程度上可以与reduce一起使用,但目前我无法帮助自己。

6 个答案:

答案 0 :(得分:4)

reduce数组上使用days。对于每一天,它都会在您的tasks对象中找到一个条目,将该条目添加到累加器中,否则,返回默认条目。

这里是MDN doc on Array.reduce()

const days = ["2019-01-01", "2019-01-02", "2019-01-03"];
const tasks = {"2019-01-01": {"planned": 3, "completed": 2,}, "2019-01-03": { "planned": 1, "completed": 0 }, "2019-01-10": { "planned": 1, "completed": 1} };

const tasksNew = days.reduce((accum, day) => {
  accum[day] = tasks[day] ? tasks[day] : { planned: 0, completed: 0 };
  return accum;
}, {});

console.log(tasksNew);

答案 1 :(得分:2)

const taskNew = days.reduce((acc, day) => {
  if (!tasks[day]) {
    return {
      ...acc,
      [day]: {
        planned: 0,
        completed: 0
      }
    }
  }  
  return {
    ...acc,
    [day]: tasks[day]
  } 
}, {});

有关reduce的详细信息:https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Array/reduce

答案 2 :(得分:2)

您可以减少创建新对象的天数组。对于每一天,您将检查当天是否有任务并将其合并到结果图中;如果当天没有任务,请合并默认的空“指标”:

const days = [
  "2019-01-01", 
  "2019-01-02", 
  "2019-01-03", 
  "2019-01-04",
  "2019-01-07",
  "2019-01-08",
  "2019-01-09",
  "2019-01-10"
]

const DEFAULT = { "planned": 0, "completed": 0 } 

const tasks = {
  "2019-01-01": {"planned": 3, "completed": 2,}, 
  "2019-01-03": { "planned": 1, "completed": 0 }, 
  "2019-01-10": { "planned": 1, "completed": 1 }
}

const result = days.reduce(
  (map, day) =>
    Object.assign({}, map, { [day]: tasks[day] ? tasks[day] : DEFAULT }),
  {}
)

console.log(result)

答案 3 :(得分:1)

您可以使用reduce来将days数组映射到tasks对象中的键。在这里,我每天都在days中循环浏览,检查它是否在tasks对象中。如果是,则将当前的day作为键并将其关联的对象从tasks添加到newTasks对象。如果day不在对象中,则将默认的complete: 0planned: 0添加到附加数组中:

const days = ["2019-01-01", "2019-01-02", "2019-01-03"],
tasks = {
  "2019-01-01": {
    "planned": 3,
    "completed": 2,
  },
  "2019-01-03": {
    "planned": 1,
    "completed": 0
  },
  "2019-01-10": {
    "planned": 1,
    "completed": 1
  }
},

newTasks = days.reduce((acc, day) => 
                   day in tasks ? 
                   {...acc, [day]: tasks[day]} : 
                   {...acc, [day]: {planned: 0, complete: 0}
                 }, {});

console.log(newTasks);

答案 4 :(得分:0)

克隆tasks。将keys中的tasks存储在变量中。然后遍历days,检查keys是否没有includes() day添加对象newTasks

const days = ["2019-01-01", "2019-01-02", "2019-01-03"]

const tasks = {"2019-01-01": {"planned": 3, "completed": 2,}, "2019-01-03": { "planned": 1, "completed": 0 }, "2019-01-10": { "planned": 1, "completed": 1}}

const newTasks = JSON.parse(JSON.stringify(tasks));

const keys = Object.keys(tasks);
days.forEach(day => {
  if(!keys.includes(day))
   newTasks[day] = {completed:0,planned:0} 
   }
)
console.log(newTasks)

答案 5 :(得分:0)

最快捷的方法:

{...days.reduce((obj,d)=>({...obj,[d]:{ planned: 0, completed: 0 }}),{}), ...tasks}