使用父ID将子JSON对象移动到它们自己的对象

我对JavaScript非常陌生,正在使用Node.JS访问API并将响应存储在SQL Server中。 我正在使用“请求”和“mssql”节点包。 我不重视这些,他们似乎是我所需要的,并有良好的文件和支持。

我的问题:我有以下格式的API的JSON响应:

requests = [ { url: 'https://domain.zendesk.com/api/v2/requests/2.json', id: 2, status: 'closed', priority: 'normal', type: 'incident', subject: 'Test Ticket', description: 'Test ticket', organization_id: 10101010101, via: { channel: 'email', source: { from: { address: 'bill.bob@domain.com', name: 'Bill Bob' }, to: { name: 'Company Helpdesk', address: 'testzendesk@domain.com' }, rel: null }, }, custom_fields:[ { id: 31368658, value: null }, { id: 29221487, value: null }, { id: 31636418, value: null }, { id: 29498078, value: null }, { id: 31659217, value: null } ], requester_id: 2020202020, collaborator_ids: [], is_public: true, due_at: null, can_be_solved_by_me: false, created_at: '2015-03-05T05:55:22Z', updated_at: '2015-03-12T05:01:51Z', recipient: 'testzendesk@domain.com', followup_source_id: null, assignee_id: 30303030303, ticket_form_id: null, fields: [ { id: 31368658, value: null }, { id: 29221487, value: null }, { id: 31636418, value: null }, { id: 29498078, value: null }, { id: 31659217, value: null } ] }, { url: 'https://domain.zendesk.com/api/v2/requests/2.json', id: 3, status: 'closed', priority: 'normal', type: 'incident', subject: 'Test Ticket', description: 'Test ticket', organization_id: 10101010101, via: { channel: 'email', source: { from: { address: 'bill.bob@domain.com', name: 'Bill Bob' }, to: { name: 'Company Helpdesk', address: 'testzendesk@domain.com' }, rel: null } }, custom_fields: [ { id: 31368658, value: null }, { id: 29221487, value: null }, { id: 31636418, value: null }, { id: 29498078, value: null }, { id: 31659217, value: null } ], requester_id: 2020202020, collaborator_ids: [], is_public: true, due_at: null, can_be_solved_by_me: false, created_at: '2015-03-05T05:55:22Z', updated_at: '2015-03-12T05:01:51Z', recipient: 'testzendesk@domain.com', followup_source_id: null, assignee_id: 30303030303, ticket_form_id: null, fields: [ { id: 31368658, value: null }, { id: 29221487, value: null }, { id: 31636418, value: null }, { id: 29498078, value: null }, { id: 31659217, value: null } ] } ]; 

我需要拉出子对象,即“通过”,“custom_fields”和“领域”与父母的ID。 因此,对于第一个对象,每个“via”子对象也将具有2的ID,并且将具有“通道”,“源”和“ID”元素。

像这样的东西:

父母:

 [ { url: 'https://domain.zendesk.com/api/v2/requests/2.json', id: 2, status: 'closed', priority: 'normal', type: 'incident', subject: 'Test Ticket', description: 'Test ticket', organization_id: 10101010101, requester_id: 2020202020, collaborator_ids: [], is_public: true, due_at: null, can_be_solved_by_me: false, created_at: '2015-03-05T05:55:22Z', updated_at: '2015-03-12T05:01:51Z', recipient: 'testzendesk@domain.com', followup_source_id: null, assignee_id: 30303030303, ticket_form_id: null }, { url: 'https://domain.zendesk.com/api/v2/requests/2.json', id: 3, status: 'closed', priority: 'normal', type: 'incident', subject: 'Test Ticket', description: 'Test ticket', organization_id: 10101010101, requester_id: 2020202020, collaborator_ids: [], is_public: true, due_at: null, can_be_solved_by_me: false, created_at: '2015-03-05T05:55:22Z', updated_at: '2015-03-12T05:01:51Z', recipient: 'testzendesk@domain.com', followup_source_id: null, assignee_id: 30303030303, ticket_form_id: null } ] 

通过:

 [ { channel: 'email', parent_id: 2 }, { channel: 'email', parent_id: 3 } ] 

via_source_from:

 [ { address: 'bill.bob@domain.com', name: 'Bill Bob', parent_id: 2 }, { address: 'bill.bob@domain.com', name: 'Bill Bob', parent_id: 2 } ] 

via_source_to:

 [ { name: 'Company Helpdesk', address: 'testzendesk@domain.com', parent_id: 2 }, { name: 'Company Helpdesk', address: 'testzendesk@domain.com', parent_id: 2 } ] 

custom_fields:

 [ { parent_id: 2, id: 31368658, value: null }, { parent_id: 2, id: 29221487, value: null }, { parent_id: 2, id: 31636418, value: null }, { parent_id: 2, id: 29498078, value: null }, { parent_id: 2, id: 31659217, value: null }, { parent_id: 3, id: 31368658, value: null }, { parent_id: 3, id: 29221487, value: null }, { parent_id: 3, id: 31636418, value: null }, { parent_id: 3, id: 29498078, value: null }, { parent_id: 3, id: 31659217, value: null } ] 

字段:

 [ { parent_id: 2, id: 31368658, value: null }, { parent_id: 2, id: 29221487, value: null }, { parent_id: 2, id: 31636418, value: null }, { parent_id: 2, id: 29498078, value: null }, { parent_id: 2, id: 31659217, value: null }, { parent_id: 3, id: 31368658, value: null }, { parent_id: 3, id: 29221487, value: null }, { parent_id: 3, id: 31636418, value: null }, { parent_id: 3, id: 29498078, value: null }, { parent_id: 3, id: 31659217, value: null } ] 

我已经四处搜寻,并没有发现任何事情可以让我这样做。

谢谢一堆!

你可以做这样的事情,没有太多的描述,但如果在下面的代码中不能理解任何东西,请评论。

我没有操纵parents对象,但是你可以自己删除所需的字段。 你可以参考这个链接 。 但是请记得在克隆对象之后操作,因为delete操作符会改变原始对象。

 let parents = [], via = [], via_source_from = [], via_source_to = [], custom_fields = [], fields = []; requests.forEach( record => { let pid = record.id; parents = [ ...parents, record ]; via = [ ...via, Object.assign({}, { parent_id: pid, channel: record.via.channel } ) ]; via_source_from = [ ...via_source_from, Object.assign({}, { parent_id: pid }, record.via.source.from ) ] via_source_to = [ ...via_source_to, Object.assign({}, { parent_id: pid }, record.via.source.to ) ] custom_fields = [ ...custom_fields, ...record.custom_fields.map( f => { return Object.assign({}, f, { parent_id: pid }) } ) ] fields = [ ...fields, ...record.fields.map( f => { return Object.assign({}, f, { parent_id: pid }) } ) ] }); console.log("parent: ", parent); console.log("via: ", via); console.log("via_source_from: ", via_source_from); console.log("via_source_to: ", via_source_to); console.log("custom_fields: ", custom_fields); console.log("fields: ", fields); 

更新

我刚刚创build了一个空数组,在每次requests迭代中添加特定的数据。 然后使用以下给出的四个概念连接数组和相关数据。

传播运营商

 var arr1 = [0, 1, 2]; var arr2 = [3, 4, 5]; var arr3 = [...arr1, ...arr2]; // returns new array [0, 1, 2, 3, 4, 5] var arr4 = [...arr3, 6]; // returns new array [0, 1, 2, 3, 4, 5, 6] 

这是javascript的新扩散运算符。 你可以参考这个链接以获得更好的理解。 在数组的情况下,它的作用与array.concat相同,只是更干净的语法。

对象分配

Object.assign()方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象。 它会返回目标对象。

 var obj = { a: 1 }; var copy = Object.assign({}, obj, { b: 2 }); // we can add as many object as we need. copy.c = 3; console.log(obj); // { a: 1 } console.log(copy); // { a: 1, b: 2, c: 3 } 

克隆对象而不参考原始对象的好方法。 总之,要创build不可变的对象。 你可以参考这个链接以获得更好的理解。

Array.prototype.map()

map()方法创build一个新的数组,其结果是对调用数组中的每个元素调用一个提供的函数。

 var numbers = [1, 5, 10, 15]; var doubles = numbers.map(function(x) { return x * 2; }); // doubles is now [2, 10, 20, 30] // numbers is still [1, 5, 10, 15] 

请记住,它返回一个新的数组,不会影响原始数组。 你必须从内部函数返回一些东西,否则你将会在最后一个数组中的那个索引处有undefined 。 你可以参考这个链接以获得更好的理解。

箭头function

箭头函数expression式比函数expression式具有更短的语法,并且不会绑定自己的this,arguments,super或new.target。

 [ 'hi', 'ola', 'hello' ].map( greet => greet.length ); // returns [ 2, 3, 5 ] 

只是写一个函数的较短的语法。 其中一个要点是它不像function关键字那样绑定它自己,它确实有助于定义this范围。 你可以参考这个链接以获得更好的理解。