JSFiddle - React, Tailwind, and code Playground

by vaheed akhtar

JavaScript

let emp = {
  name: 'Anshu',
  address: {
    office: {
      city: 'BLR',
      state: 'KARNATAKA'
    },
    home: {
      addres1: {
        city: 'PATNA',
        state: 'BIHAR'
      },
      address2: {
        city: 'BLR',
        state: 'KARNTKa',
        zipcode: {
          pin: 560043
        }
      }
    }
  }
}
  let result = {};
function flattenObj(obj, key) {
  for(let i in obj) {
    if(typeof obj[i] === "object")
      flattenObj(obj[i], key + '_' + i);
    else
     result[key + '_' + i] = obj[i];
  }
  return result;
}

console.log(flattenObj(emp, 'emp'));