如果该对象的键与第二个对象的键匹配,如何填充该对象的值?
\n要填充的对象
\nconst task = {\n name: '',\n description: '',\n status: '',\n priority: '',\n due_date: '',\n due_date_time: '',\n parent: '',\n time_estimate: '',\n start_date: '',\n start_date_time: '',\n assignees: {},\n archived: ''\n }\nRun Code Online (Sandbox Code Playgroud)\n原始对象
\nconst taskData = { \n id: '476ky1',\n custom_id: null,\n name: 'Reuni\xc3\xa3o com o novo Gerente de Vendas - Airton',\n text_content: null,\n description: null,\n status: \n { id: 'p3203621_11svBhbO',\n status: 'to do',\n color: '#d3d3d3',\n orderindex: 0,\n type: 'open' },\n orderindex: '1.16183176837360000000000000000000',\n date_created: '1618317683783',\n date_updated: '1618317683783',\n date_closed: null,\n archived: false,\n creator: \n { id: 3184709,\n username: 'Michael Jackson',\n color: '#455963',\n email: 'email@gmail.com',\n profilePicture: null },\n assignees: \n [ { id: 3184709,\n username: 'Antonio Santos',\n color: '#455963',\n initials: 'AS',\n email: 'santosonit@gmail.com',\n profilePicture: null } ],\n watchers: \n [ { id: 3184709,\n username: 'Antonio Santos',\n color: '#455963',\n initials: 'AS',\n email: 'santosonit@gmail.com',\n profilePicture: null } ],\n checklists: [],\n tags: [],\n parent: null,\n priority: null,\n due_date: null,\n start_date: null,\n points: null,\n time_estimate: null,\n time_spent: 0,\n custom_fields: [],\n dependencies: [],\n linked_tasks: [],\n team_id: '3101702',\n url: 'https://app.clickup.com/t/476ky1',\n permission_level: 'create',\n list: { id: '13700791', name: 'List', access: true },\n project: { id: '7328469', name: 'hidden', hidden: true, access: true },\n folder: { id: '7328469', name: 'hidden', hidden: true, access: true },\n space: { id: '3203621' },\n attachments: [] \n}\nRun Code Online (Sandbox Code Playgroud)\n现在,先不要谴责我,因为我刚刚开始处理对象,我确信有很多方法可以做到这一点,但是有for loop一种方法可以看吗?它不会修改task对象,对吧。那么我该如何返回task填充呢?
for(let a = 0; a < task.length; a++){\n for (let n = 0; n < data.length; a++){\n if(Object.keys(task) == Object.keys(data)){\n Object.values(task) = Object.values(data)\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n感谢你的帮助!
\n您已经获得了想要在 中定义好的键task,因此我们可以使用 简单地循环它们for (let k in task)。如果taskData[k]没有值,此解决方案将恢复为使用空字符串。(滚动到代码块底部查看解决方案!)
const taskData = { \n id: \'476ky1\',\n custom_id: null,\n name: \'Reuni\xc3\xa3o com o novo Gerente de Vendas - Airton\',\n text_content: null,\n description: null,\n status: \n { id: \'p3203621_11svBhbO\',\n status: \'to do\',\n color: \'#d3d3d3\',\n orderindex: 0,\n type: \'open\' },\n orderindex: \'1.16183176837360000000000000000000\',\n date_created: \'1618317683783\',\n date_updated: \'1618317683783\',\n date_closed: null,\n archived: false,\n creator: \n { id: 3184709,\n username: \'Michael Jackson\',\n color: \'#455963\',\n email: \'email@gmail.com\',\n profilePicture: null },\n assignees: \n [ { id: 3184709,\n username: \'Antonio Santos\',\n color: \'#455963\',\n initials: \'AS\',\n email: \'santosonit@gmail.com\',\n profilePicture: null } ],\n watchers: \n [ { id: 3184709,\n username: \'Antonio Santos\',\n color: \'#455963\',\n initials: \'AS\',\n email: \'santosonit@gmail.com\',\n profilePicture: null } ],\n checklists: [],\n tags: [],\n parent: null,\n priority: null,\n due_date: null,\n start_date: null,\n points: null,\n time_estimate: null,\n time_spent: 0,\n custom_fields: [],\n dependencies: [],\n linked_tasks: [],\n team_id: \'3101702\',\n url: \'https://app.clickup.com/t/476ky1\',\n permission_level: \'create\',\n list: { id: \'13700791\', name: \'List\', access: true },\n project: { id: \'7328469\', name: \'hidden\', hidden: true, access: true },\n folder: { id: \'7328469\', name: \'hidden\', hidden: true, access: true },\n space: { id: \'3203621\' },\n attachments: [] \n};\n\nconst task = {\n name: \'\',\n description: \'\',\n status: \'\',\n priority: \'\',\n due_date: \'\',\n due_date_time: \'\',\n parent: \'\',\n time_estimate: \'\',\n start_date: \'\',\n start_date_time: \'\',\n assignees: {},\n archived: \'\'\n};\n\n// Here\'s the solution!\nfor (let k in task) task[k] = taskData[k] ?? \'\';\n\nconsole.log({ task });Run Code Online (Sandbox Code Playgroud)\r\n请注意,如果您确定taskData包含 中键的超集task,则可以简单地使用:
for (let k in task) task[k] = taskData[k];\nRun Code Online (Sandbox Code Playgroud)\n
如果 中的所有键都task存在于 中taskData,则可以使用以下方式迭代键forEach复制数据:
Object.keys(task).forEach(k => task[k] = taskData[k])\nRun Code Online (Sandbox Code Playgroud)\n笔记
\n这将复制对其中不是原语的任何内容的引用taskData,因此,如果您修改其中之一,task也会更改 中的值taskData。如果这不是所需的行为,您应该使用本问答中描述的方法之一进行深层复制。
const task = {\n name: \'\',\n description: \'\',\n status: \'\',\n priority: \'\',\n due_date: \'\',\n due_date_time: \'\',\n parent: \'\',\n time_estimate: \'\',\n start_date: \'\',\n start_date_time: \'\',\n assignees: {},\n archived: \'\'\n }\n \nconst taskData = { \n id: \'476ky1\',\n custom_id: null,\n name: \'Reuni\xc3\xa3o com o novo Gerente de Vendas - Airton\',\n text_content: null,\n description: null,\n status: \n { id: \'p3203621_11svBhbO\',\n status: \'to do\',\n color: \'#d3d3d3\',\n orderindex: 0,\n type: \'open\' },\n orderindex: \'1.16183176837360000000000000000000\',\n date_created: \'1618317683783\',\n date_updated: \'1618317683783\',\n date_closed: null,\n archived: false,\n creator: \n { id: 3184709,\n username: \'Michael Jackson\',\n color: \'#455963\',\n email: \'email@gmail.com\',\n profilePicture: null },\n assignees: \n [ { id: 3184709,\n username: \'Antonio Santos\',\n color: \'#455963\',\n initials: \'AS\',\n email: \'santosonit@gmail.com\',\n profilePicture: null } ],\n watchers: \n [ { id: 3184709,\n username: \'Antonio Santos\',\n color: \'#455963\',\n initials: \'AS\',\n email: \'santosonit@gmail.com\',\n profilePicture: null } ],\n checklists: [],\n tags: [],\n parent: null,\n priority: null,\n due_date: null,\n start_date: null,\n points: null,\n time_estimate: null,\n time_spent: 0,\n custom_fields: [],\n dependencies: [],\n linked_tasks: [],\n team_id: \'3101702\',\n url: \'https://app.clickup.com/t/476ky1\',\n permission_level: \'create\',\n list: { id: \'13700791\', name: \'List\', access: true },\n project: { id: \'7328469\', name: \'hidden\', hidden: true, access: true },\n folder: { id: \'7328469\', name: \'hidden\', hidden: true, access: true },\n space: { id: \'3203621\' },\n attachments: [] \n}\n\nObject.keys(task).forEach(k => task[k] = taskData[k])\n\nconsole.log(task)Run Code Online (Sandbox Code Playgroud)\r\n