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

201
Views
Flatten nested JSON second level only

I have my JSON structured like this

var data = var data = [{"id": 451, "title": {    "rendered": "title1" }, "acf": {    "floor": "6",    "business": [       "business"    ],    "status": {       "value": "null",       "label": "Määramata"    },    "suund": "",    "area": {       "value": "full",       "label": "Terve korrus"       },       "suurus": "",       "tookohad": "",    }, },]; 

I need to flatten acf to get it like this without acf nesting.

var data = [
  {
"id": 451,
"title": {
   "rendered": "Title 1"
},
   "floor": "6",
   "business": [
      "business"
   ],
   "status": {
      "value": "null",
      "label": "Määramata"
   },
   "suund": "",
   "area": {
      "value": "full",
      "label": "Full"
      },
      "size": "",
},
];

I tried to use this function

function flat(source, target) {
Object.keys(source).forEach(function (k) {
    if (source[k]!== null && typeof source[k] === 'object') {
        flat(source[k], target);
        return;
    }
    target[k] = source[k];
});flatObject = {};flat(data, flatObject);console.log(flatObject);

But it flattens everything. I need to pick stuff out of acf but keep elements with id separate.

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

0

You can do the following:

const flattenData = {
  ...data,
  acf: undefined,
  ...data.acf
};
delete flattenData['acf'];

This method chains some destructuring assignments. First - the ...data - we copy all the data in the first level. Then in the new object we overwrite acf with undefined. This reduces the memory footprint. At last we overwrite/copy the complete object of data.acf, thus flatten the array in the way you want.

If you can live with the acf: undefined in the array, you can remove the delete flattenData['acf'] and make it a bit faster.

about 4 years ago · Juan Pablo Isaza Report

0

As I understand what you want to achieve is this: ( I improved the example so that you can work even on an array of objects )

var data = [{"id": 451, "title": {    "rendered": "title1" }, "acf": {    "floor": "6",    "business": [       "business"    ],    "status": {       "value": "null",       "label": "Määramata"    },    "suund": "",    "area": {       "value": "full",       "label": "Terve korrus"       },       "suurus": "",       "tookohad": "",    }, },]; 

function flat(source) {
  let result = source.map(el => {
    let noAcf = {}
    Object.keys(el).forEach(key => {
      if(key === "acf"){
        noAcf = {...noAcf, ...el[key]}
      } else {
        noAcf[key]=el[key]
      }
    })
    return noAcf
  })
  return result
}


const flatObjectsList = flat(data);
console.log(flatObjectsList);

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!