JavaScript flattenObject
function that traverses and flattens any object
This recursive function accepts two arguments: objectToFlat
and flatObject
and returns flatObject
containing key/value pairs from objectToFlat
flattened to a single level object. This function traverses nested objects to the lowest level until there are no more nested objects within object.
- Parent key/value objects will be lost if there's any following child object with the same key name. Remember? We can't have object with duplicate key names!
- Key names from parent objects will be lost forever. There's nowhere to go!
- Nested objects containing arrays will be lost
Let's process this multi-level object and get it flattened:
let objectToFlat = {
title: "Terminator 2",
writtenBy: [
"James Cameron",
"William Wisher Jr."
],
year: 1991,
aired: true,
cast: {
mainRoles: {
"Arnold Schwarzenegger": "The Terminator",
"Linda Hamilton": "Sarah Connor"
},
otherRoles: {
"Ron Young": "Pool Cue Biker",
"Mike Muscat": "Moshier",
evenMoreRoles: {
"Abdul Salaam El Razzac": "Gibbons"
}
}
},
}
with the result being:
{
"title": "Terminator 2",
"year": 1991,
"aired": true,
"Arnold Schwarzenegger": "The Terminator",
"Linda Hamilton": "Sarah Connor",
"Ron Young": "Pool Cue Biker",
"Mike Muscat": "Moshier",
"Abdul Salaam El Razzac": "Gibbons"
}
const flattenObject = require('./js-flatten-object)
let flatObject = flattenObject(objectToFlat, {})
flattenObject(objectToFlat, flatObject)
objectToFlat
- object to flat
flatObject
- object where flattened key/value pairs will be stored
flatObject
containing flattened objectToFlat
object