Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

131
Views
How to restructure the JSON Object with new key values?

Please help me to recreate the JSON object received from API, refer below JSON object from API below is Input JSON

{
  'Demo-1': [
    {
      sku: 'Brisk',
      count: '2',
    },
    {
      sku: 'Pepsi Cans',
      count: '2',
    },
    {
      sku: 'Pepsi Cans',
      count: '4',
    }
    
  ],
  
  'Demo-2' : [
    {
      sku: 'Mountain',
      count: '4',
    },
    {
      sku: 'Pepsi Bottles',
      count: '4',
    },
    {
      sku: 'Lipton Dietgreentea',
      count: '2',
    },
    {
      sku: 'Lipton Dietgreentea Mixedberry',
      count: '2',
    }
  ]
}

from this to convert below JSON object with additional keys, and restructure the array items inside the another key

Result should be like

{
    'Demo-1': {
        items: [{
                sku: 'Brisk',
                count: '2',
            },
            {
                sku: 'Pepsi',
                count: '2',
            },
            {
                sku: 'Pepsi',
                count: '4',
            }

        ],
        mode: "Veri",
        status: "open"

    },
    'Demo-2': {
        "items": [{
                sku: 'Mountain',
                count: '4',
            },
            {
                sku: 'Pepsi Bottles',
                count: '4',
            },
            {
                sku: 'Lipton Dietgreentea',
                count: '2',
            },
            {
                sku: 'Lipton Dietgreentea Mixedberry',
                count: '2',
            }
        ],
        mode: "Clear",
        status: "Closed"
    }
}

I tried in angular but I cant achieve the result, please look out the code below

 Object.entries(_data).map((items,idx)=>{
     
      this._newPickLists = {
        items[0] :{
          items : items[1],
          mode : 'Verification',
          status : 'open'
        }
      }

    
    })

Thanks in Advance

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Using Object.entries and Object.fromEntries, you can just change the value to the object with items as the key

const obj = {"Demo-1":[{"sku":"Brisk","count":"2"},{"sku":"Pepsi Cans","count":"2"},{"sku":"Pepsi Cans","count":"4"}],"Demo-2":[{"sku":"Mountain","count":"4"},{"sku":"Pepsi Bottles","count":"4"},{"sku":"Lipton Dietgreentea","count":"2"},{"sku":"Lipton Dietgreentea Mixedberry","count":"2"}]};

console.log(
  Object.fromEntries(
    Object.entries(obj).map(
      ([key, items]) => [key, { items, mode: "verification", status: "open" }]
    )
  )
)

about 4 years ago · Juan Pablo Isaza Report

0

Just replace the newObject with the variable in your app.

const newObject = {};

for (const [key, value] of Object.entries(obj)) {
    newObject[key] = {};
    newObject[key]["items"] = value;
    newObject[key].mode = "Clear";
    newObject[key].status = "Closed";

}

console.log(newObject);


about 4 years ago · Juan Pablo Isaza Report

0

Here is a solution that will update the original data object:

  Object.keys(data).forEach((key)=> { 
     data[key] = {items: data[key], mode: 'Veri', status: 'open' }}
  )
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!