javascript - 如何迭代两个对象并根据 JS 中的另一个更新一个值?

如果该对象的键与第二个对象的键匹配,我如何填充该对象的值?

要填充的对象

const task = {
      name: '',
      description: '',
      status: '',
      priority: '',
      due_date: '',
      due_date_time: '',
      parent: '',
      time_estimate: '',
      start_date: '',
      start_date_time: '',
      assignees: {},
      archived: ''
  }

原始对象

const taskData = { 
  id: '476ky1',
  custom_id: null,
  name: 'Reunião com o novo Gerente de Vendas - Airton',
  text_content: null,
  description: null,
  status: 
   { id: 'p3203621_11svBhbO',
     status: 'to do',
     color: '#d3d3d3',
     orderindex: 0,
     type: 'open' },
  orderindex: '1.16183176837360000000000000000000',
  date_created: '1618317683783',
  date_updated: '1618317683783',
  date_closed: null,
  archived: false,
  creator: 
   { id: 3184709,
     username: 'Michael Jackson',
     color: '#455963',
     email: 'email@gmail.com',
     profilePicture: null },
  assignees: 
   [ { id: 3184709,
       username: 'Antonio Santos',
       color: '#455963',
       initials: 'AS',
       email: 'santosonit@gmail.com',
       profilePicture: null } ],
  watchers: 
   [ { id: 3184709,
       username: 'Antonio Santos',
       color: '#455963',
       initials: 'AS',
       email: 'santosonit@gmail.com',
       profilePicture: null } ],
  checklists: [],
  tags: [],
  parent: null,
  priority: null,
  due_date: null,
  start_date: null,
  points: null,
  time_estimate: null,
  time_spent: 0,
  custom_fields: [],
  dependencies: [],
  linked_tasks: [],
  team_id: '3101702',
  url: 'https://app.clickup.com/t/476ky1',
  permission_level: 'create',
  list: { id: '13700791', name: 'List', access: true },
  project: { id: '7328469', name: 'hidden', hidden: true, access: true },
  folder: { id: '7328469', name: 'hidden', hidden: true, access: true },
  space: { id: '3203621' },
  attachments: [] 
}

现在,请不要谴责我,因为我才刚刚开始处理对象,我确信有很多方法可以做到这一点,但是 for loop 是一种方法吗?看着?它不会修改 task 对象,对吧。那么我将如何返回填充的task呢?

for(let a = 0; a < task.length; a++){
  for (let n = 0; n < data.length; a++){
    if(Object.keys(task) == Object.keys(data)){
      Object.values(task) = Object.values(data)
    }
  }
}

感谢您的帮助!

最佳答案

您已经在 task 中很好地定义了您想要的键,因此我们可以简单地使用 for (let k in task) 遍历它们。如果 taskData[k] 没有值,此解决方案将恢复为使用空字符串。 (滚动到代码块的底部以查看解决方案!)

const taskData = { 
  id: '476ky1',
  custom_id: null,
  name: 'Reunião com o novo Gerente de Vendas - Airton',
  text_content: null,
  description: null,
  status: 
   { id: 'p3203621_11svBhbO',
     status: 'to do',
     color: '#d3d3d3',
     orderindex: 0,
     type: 'open' },
  orderindex: '1.16183176837360000000000000000000',
  date_created: '1618317683783',
  date_updated: '1618317683783',
  date_closed: null,
  archived: false,
  creator: 
   { id: 3184709,
     username: 'Michael Jackson',
     color: '#455963',
     email: 'email@gmail.com',
     profilePicture: null },
  assignees: 
   [ { id: 3184709,
       username: 'Antonio Santos',
       color: '#455963',
       initials: 'AS',
       email: 'santosonit@gmail.com',
       profilePicture: null } ],
  watchers: 
   [ { id: 3184709,
       username: 'Antonio Santos',
       color: '#455963',
       initials: 'AS',
       email: 'santosonit@gmail.com',
       profilePicture: null } ],
  checklists: [],
  tags: [],
  parent: null,
  priority: null,
  due_date: null,
  start_date: null,
  points: null,
  time_estimate: null,
  time_spent: 0,
  custom_fields: [],
  dependencies: [],
  linked_tasks: [],
  team_id: '3101702',
  url: 'https://app.clickup.com/t/476ky1',
  permission_level: 'create',
  list: { id: '13700791', name: 'List', access: true },
  project: { id: '7328469', name: 'hidden', hidden: true, access: true },
  folder: { id: '7328469', name: 'hidden', hidden: true, access: true },
  space: { id: '3203621' },
  attachments: [] 
};

const task = {
    name: '',
    description: '',
    status: '',
    priority: '',
    due_date: '',
    due_date_time: '',
    parent: '',
    time_estimate: '',
    start_date: '',
    start_date_time: '',
    assignees: {},
    archived: ''
};

// Here's the solution!
for (let k in task) task[k] = taskData[k] ?? '';

console.log({ task });

请注意,如果您确定 taskData 包含 task 中键的超集,您可以简单地使用:

for (let k in task) task[k] = taskData[k];

https://stackoverflow.com/questions/74001211/

相关文章:

r - 与 R 中的日期相关联的条件累积和

javascript - 过滤 2 个数组以检查父子是否

regex - 使用 awk 只包含以特定值开头的列?

user-interface - 愚蠢且令人沮丧的跨浏览器 UI 问题

java - @EnableGlobalMethodSecurity 在新的 spring boot

c++ - 使用 enable_if 对类方法进行部分模板特化

java - 流迭代不使用最后一个值

rust - 将闭包指定为返回类型的替代方法?

c++ - 为什么即使还有弱指针,make_shared 也会调用析构函数?

.net - 投票 : What do you call your business layer b