簡體   English   中英

JS - 將嵌套的 JSON 數組轉換為單個對象

[英]JS - Transform Nested array of JSON into single Object

我想將嵌套的 JSON 結構轉換為單個對象,使用我在下面的代碼中嘗試過的動態鍵,但它僅適用於一個級別,我需要編寫一些遞歸函數,我正在努力編寫 n 級的代碼嵌套的 JSON。 請指教。

   data.map((e) => {
   for (let key in e) {
     if (typeof e[key] === "object") {
       for (let onLevel in e[key]) {
         e[key + "." + onLevel] = e[key][onLevel];
       }
     }
   }
 });

例子

輸入 JSON

[{
  "Id": "0hb3L00000000jkQAA",
  "Name": "P-2797",
  "ContactEncounterId": "0ha3L000000001qQAA",
  "StartTime": "2020-06-27T11:00:00.000Z",
  "EncounterDuration": 25,
  "ContactEncounter": {
    "Name": "Grocery Shopping 17",
    "LocationId": "1313L0000004ENlQAM",
    "Id": "0ha3L000000001qQAA",
    "Location": {
      "Name": "Waitrose",
      "LocationType": "Site",
      "Id": "1313L0000004ENlQAM"
    }
  }
}]

輸出 JSON

[{
  "Id": "0hb3L00000000jkQAA",
  "Name": "P-2797",
  "ContactEncounterId": "0ha3L000000001qQAA",
  "StartTime": "2020-06-27T11:00:00.000Z",
  "EncounterDuration": 25,
  "ContactEncounter.Name": "Grocery Shopping 17",
  "ContactEncounter.LocationId": "1313L0000004ENlQAM",
  "ContactEncounter.Id": "0ha3L000000001qQAA",
  "ContactEncounter.Location.Name": "Waitrose",
  "ContactEncounter.Location.LocationType": "Site",
  "ContactEncounter.Location.Id": "1313L0000004ENlQAM"
}]

正如你所說,你需要創建一個遞歸來更深入地了解對象。 這意味着,您必須跟蹤您所在的路徑。

你可以通過以下方式解決

 const input = [{ "Id": "0hb3L00000000jkQAA", "Name": "P-2797", "ContactEncounterId": "0ha3L000000001qQAA", "StartTime": "2020-06-27T11:00:00.000Z", "EncounterDuration": 25, "ContactEncounter": { "Name": "Grocery Shopping 17", "LocationId": "1313L0000004ENlQAM", "Id": "0ha3L000000001qQAA", "Location": { "Name": "Waitrose", "LocationType": "Site", "Id": "1313L0000004ENlQAM" } } } ]; function merge( source, target = {}, ...parents) { for (let [key, value] of Object.entries( source ) ) { const path = (parents || []).concat( key ); if (typeof value === 'object') { merge( value, target, ...path ); continue; } target[path.join('.')] = value; } return target; } console.log( merge( input[0] ) );

或者通過以下方式,您只需使用Object.assign將更深層次搜索的結果分配到當前對象中。

 const input = [{ "Id": "0hb3L00000000jkQAA", "Name": "P-2797", "ContactEncounterId": "0ha3L000000001qQAA", "StartTime": "2020-06-27T11:00:00.000Z", "EncounterDuration": 25, "ContactEncounter": { "Name": "Grocery Shopping 17", "LocationId": "1313L0000004ENlQAM", "Id": "0ha3L000000001qQAA", "Location": { "Name": "Waitrose", "LocationType": "Site", "Id": "1313L0000004ENlQAM" } } } ]; function merge( source, ...parents) { const mergedValue = {}; for (let [key, value] of Object.entries( source ) ) { const path = (parents || []).concat( key ); if (typeof value === 'object') { Object.assign( mergedValue, merge( value, ...path ) ); continue; } mergedValue[path.join('.')] = value; } return mergedValue; } console.log( merge( input[0] ) );

這是另一種使用第二個參數並在查找第 n 級對象時傳遞鍵的方法。

 const obj = { "Id": "0hb3L00000000jkQAA", "Name": "P-2797", "ContactEncounterId": "0ha3L000000001qQAA", "StartTime": "2020-06-27T11:00:00.000Z", "EncounterDuration": 25, "ContactEncounter": { "Name": "Grocery Shopping 17", "LocationId": "1313L0000004ENlQAM", "Id": "0ha3L000000001qQAA", "Location": { "Name": "Waitrose", "LocationType": "Site", "Id": "1313L0000004ENlQAM" } } } function flattenObj(obj, param) { let newObj = {}; for (let key in obj) { if (typeof obj[key] === 'object') { newObj = { ...newObj, ...flattenObj(obj[key], key + '.') } } else { newObj[param + key] = obj[key] } } return newObj; } console.log(flattenObj(obj, ''))

暫無
暫無

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

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