简体   繁体   中英

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

I've tried a couple hours to find a way to handle JSON like this:

[
  {
    "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
  }
]

I want to add an array Below NameCategory just in case the NameCategory value is duplicate, so the expected result would be:

[
  {
    "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
      }
    ]
  }
]

I'm not familiar to handling JSON, so I need help here,

anyone can help me how to handling those json?

Use reduce method. This case return a new array , while creating this new array of objects check if there exist an object whose name is same as the NamaCategory of the old array using findIndex . findIndex will return -1 if this NamaCategory does not exist in the new array. If it does not exist create an object with desired value and push it to the new array. If NamaCategory exists then just update the value array

 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); 

Just loop through data and check that if element exists in final array, If exists push the value to values property, if not create a new property in the final array.

 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} 

I've commented the code. Does this require any further explanation besides the comments in the code? Well, you'd have to get familiar with object destructuring

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM