Click here to Skip to main content
15,446,232 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I have below sample JSON.
{
  "SCORE": {
    "Multi-Choice": [
      {
        "VALUE": "Strong",
        "DUCED": 1
      },
      {
        "VALUE": "Good",
        "DUCED": 2
      },
      {
        "VALUE": "Satisfactory",
        "DUCED": 3
      },
      {
        "VALUE": "Weak",
        "DUCED": 4
      },
      {
        "VALUE": "Default",
        "DUCED": 5
      },
      {
        "VALUE": "None",
        "DUCED": 6
      }
    ]
  },
  "TEST_LIST": {
    "Enumerated": [
      {
        "VALUE": "Strong"
      },
      {
        "VALUE": "Good"
      },
      {
        "VALUE": "Satisfactory"
      },
      {
        "VALUE": "Weak"
      },
      {
        "VALUE": "Default"
      },
      {
        "VALUE": "None"
      }
    ]
  }
}


For this I have below code.

let finalArr = [];
              for (let [key, value] of Object.entries(resObj)) {
                if (typeof value === 'object') {
                  for (let [keyInternal, valueInternal] of Object.entries(value)) {
                    valueInternal.map(arrValue => {
                      const finalObj = {
                        NAME: '',
                        VALUE: '',
                        TYPE: ''
                      };
                      finalObj.NAME = key;
                      finalObj.VALUE = arrValue;
                      finalObj.TYPE = keyInternal;
                      finalArr.push(finalObj);
                      console.log(finalArr);
                    });
                  }
                } 
                }


Above code giving me an output in below format. `
{
            "NAME": "SCORE",
            "VALUE": {
                "VALUE": "Strong",
                "DUCED": 1
            },
            "TYPE": "Multi-Choice"
        }


But I want to display it using below format.

{
            "NAME": "SCORE",
            "VALUE": "Strong",
            "DUCED": 1,
            "TYPE": "Multi-Choice"
        }


What sorting logic I am missing here not able to fine. Thanks in advance.

What I have tried:

I am trying to merge two JSON array but still output is not expected.

<pre>let finalArr = [];
              for (let [key, value] of Object.entries(resObj)) {
                if (typeof value === 'object') {
                  for (let [keyInternal, valueInternal] of Object.entries(value)) {
                    valueInternal.map(arrValue => {
                      const finalObj = {
                        RANGESET_NAME: '',
                        //RANGESET_VALUE: '',
                        RANGE_TYPE: ''
                      };
                      finalObj.NAME = key;
                      finalObj.TYPE = keyInternal;
                      finalArr.push(finalObj);
                      finalArr.push(arrValue);
                    });
                  }
                } 
                }
Posted
Updated 12-Mar-20 1:25am
v2

1 solution

Check this out: Sorting a JSON array according one property in JavaScript[^]

JavaScript
function sortByProperty(property){  
   return function(a,b){  
      if(a[property] > b[property])  
         return 1;  
      else if(a[property] < b[property])  
         return -1;  
  
      return 0;  
   }  
}


Usage:
JavaScript
items.sort(sortByProperty("pId")); //sort according to pId 
 
items.sort(sortByProperty("id")); //sort according to id
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900