簡體   English   中英

JS - 如何將相同的對象值推送到數組中

[英]JS - How do you push same object value in to an array

我已經嘗試了幾個小時來找到一種處理JSON的方法,如下所示:

[
  {
    "value": "Osteonecrosis",
    "Diagnosis_Code": "DIAG002",
    "NamaCategory": "Primary Category",
    "FK_Diagnosis_Content_ID": 2
  },
  {
    "value": "Malunion",
    "Diagnosis_Code": "DIAG002",
    "NamaCategory": "Healing",
    "FK_Diagnosis_Content_ID": 19
  },
  {
    "value": "Osteonecrosis",
    "Diagnosis_Code": "DIAG004",
    "NamaCategory": "Primary Category",
    "FK_Diagnosis_Content_ID": 2
  },
  {
    "value": "Malunion",
    "Diagnosis_Code": "DIAG004",
    "NamaCategory": "Healing",
    "FK_Diagnosis_Content_ID": 19
  }
]

我想在NameCategory下面添加一個數組,以防NameCategory值重復,所以預期的結果是:

[
  {
    "NamaCategory": "Primary Category",
    "value":[
      {
        "value": "Osteonecrosis",
        "Diagnosis_Code": "DIAG002",
        "FK_Diagnosis_Content_ID": 2
      },
      {
        "value": "Osteonecrosis",
        "Diagnosis_Code": "DIAG004",
        "FK_Diagnosis_Content_ID": 2
      }
    ]
  },
  {
    "NamaCategory": "Healing",
    "value":[
      {
        "value": "Malunion",
        "Diagnosis_Code": "DIAG002",
        "FK_Diagnosis_Content_ID": 19
      },
      {
        "value": "Malunion",
        "Diagnosis_Code": "DIAG004",
        "FK_Diagnosis_Content_ID": 19
      }
    ]
  }
]

我對處理JSON並不熟悉,所以我需要幫助,

任何人都可以幫助我如何處理這些json?

使用reduce方法。 這種情況返回一個新數組,而創建這個新的對象數組時,使用findIndex檢查是否存在一個名稱與舊數組的NamaCategory相同的對象。 如果新數組中不存在此NamaCategoryfindIndex將返回-1 如果它不存在,請創建具有所需值的對象並將其推送到新數組。 如果存在NamaCategory ,則只需更新值數組

 var orgArray = [{"value":"Osteonecrosis","Diagnosis_Code":"DIAG002","NamaCategory":"Primary Category","FK_Diagnosis_Content_ID":2},{"value":"Malunion","Diagnosis_Code":"DIAG002","NamaCategory":"Healing","FK_Diagnosis_Content_ID":19},{"value":"Osteonecrosis","Diagnosis_Code":"DIAG004","NamaCategory":"Primary Category","FK_Diagnosis_Content_ID":2},{"value":"Malunion","Diagnosis_Code":"DIAG004","NamaCategory":"Healing","FK_Diagnosis_Content_ID":19}]; var newArray = orgArray.reduce(function(acc, curr) { //finding Index in the array where the NamaCategory matched var findIfNameExist = acc.findIndex(function(item) { return item.NamaCategory === curr.NamaCategory; }) // if in the new array no such object exist where // namecategory matches then create a new object if (findIfNameExist === -1) { let obj = { 'NamaCategory': curr.NamaCategory, "value": [curr] } acc.push(obj) } else { // if name category matches , then push the value acc[findIfNameExist].value.push(curr) } return acc; }, []); console.log(newArray) 

 var data = [{"value":"Osteonecrosis","Diagnosis_Code":"DIAG002","NamaCategory":"Primary Category","FK_Diagnosis_Content_ID":2},{"value":"Malunion","Diagnosis_Code":"DIAG002","NamaCategory":"Healing","FK_Diagnosis_Content_ID":19},{"value":"Osteonecrosis","Diagnosis_Code":"DIAG004","NamaCategory":"Primary Category","FK_Diagnosis_Content_ID":2},{"value":"Malunion","Diagnosis_Code":"DIAG004","NamaCategory":"Healing","FK_Diagnosis_Content_ID":19}]; var final = []; data.forEach(function(e) { var match = false; final.forEach(function(i) { if (e.NamaCategory == i.NamaCategory) { match = true; } }); if (!match) { var obj = { NamaCategory: e.NamaCategory, values: [e] } final.push(obj); } else { final.forEach(function(i) { if (e.NamaCategory == i.NamaCategory) { i.values.push(e); } }); } }); console.log(final); 

只需遍歷數據並檢查if final是否存在於最終數組中,If exists將值推送到values屬性,如果不在最終數組中創建新屬性。

 let data = [ { "value": "Osteonecrosis", "Diagnosis_Code": "DIAG002", "NamaCategory": "Primary Category", "FK_Diagnosis_Content_ID": 2 }, { "value": "Malunion", "Diagnosis_Code": "DIAG002", "NamaCategory": "Healing", "FK_Diagnosis_Content_ID": 19 }, { "value": "Osteonecrosis", "Diagnosis_Code": "DIAG004", "NamaCategory": "Primary Category", "FK_Diagnosis_Content_ID": 2 }, { "value": "Malunion", "Diagnosis_Code": "DIAG004", "NamaCategory": "Healing", "FK_Diagnosis_Content_ID": 19 } ]; //grouping by name: //first creating a map by name for that let dataByNamaCategory = {}; //iterating over the input array and using object destructuring to seperate the name from the other props data.forEach(({NamaCategory, ...otherProps}) => { //if there's already an entry by this name in the map if(NamaCategory in dataByNamaCategory){ //just push the new value dataByNamaCategory[NamaCategory].value.push(otherProps) }else{ //otherwise create a new entry on the map dataByNamaCategory[NamaCategory] = { NamaCategory, value: [ otherProps ] }; } }); //get just the values from the map let groupedData = Object.values(dataByNamaCategory); console.log(groupedData); 
 .as-console-wrapper{top:0;max-height:100%!important} 

我評論了代碼。 除了代碼中的注釋之外,這還需要進一步解釋嗎? 好吧,你必須熟悉對象解構

暫無
暫無

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

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