簡體   English   中英

將子JSON對象移動到具有父ID的自己的對象

[英]Move child JSON objects to their own object with parent ID

我對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 } 
  ] 
} ];

我需要拉出子對象,即帶有父ID的“ via”,“ custom_fields”和“ fields”。 因此,對於第一個對象,每個“ via”子對象也將具有ID 2,並具有“ channel”,“ source”和“ 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);

更新資料

我剛開始創建一個空數組,以在每次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的情況下,它與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 }

一種無需引用原始對象即可克隆對象的好方法。 簡而言之,創建不可變對象。 您可以參考此鏈接以更好地理解。

Array.prototype.map()

map()方法創建一個新數組,並在調用數組中的每個元素上調用提供的函數。

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 您可以參考此鏈接以更好地理解。

箭頭功能

箭頭函數表達式的語法比函數表達式短,並且不綁定自身的this,arguments,super或new.target。

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

只是編寫函數的較短語法。 一個要點是,它不綁定自己的thisfunction關鍵字不同,它確實有助於定義this范圍。 您可以參考此鏈接以更好地理解。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM