I have the below object inputObj
and I want to convert it to a simple object like outputObj
.
var inputObj = {
'a' : 1,
'b' : true,
'c' : 'string1',
'd' : {
'e' : 'string2',
'f' : false,
'g' : 5,
'h' : {
'i' : 7,
'j' : 'string3',
'k' : [
{
'name' : 'l',
'value': 11
},
{
'name' : 'm',
'value': {
'n' : 13,
'o' : 'string4'
}
}
]
}
},
'p' : [
{
'name' : 'q',
'value': 15
},
{
'name' : 'r',
'value': 'Awesome!'
}
]
}
var outputObj = {
'a' : 1,
'b' : true,
'c' : 'string1',
'e' : 'string2',
'f' : false,
'g' : 5,
'i' : 7,
'j' : 'string3',
'l' : 11,
'n' : 13,
'o' : 'string4',
'q' : 15,
'r' : 'Awesome!'
}
Please note that in case of array
the final output object would be build from name
and value
properties as per above example.
I have tried to implement the same using below code. I know as I am calling the nested function, the final object is getting reset and the scope is also different there. May be basic closure concept can solve this but I am confused with this. If you can correct my code or altogether new code is also fine.
function convertToFirstLevelObject( object ) {
var returnObj = {};
if( IsPrimaryDataType( object )) {
return object;
}
else {
if( object instanceof Array ) {
for ( var i = 0; i < object.length; i++ ) {
if( typeof object[i] === 'object' && !(object[i] instanceof Array) ) {
var key = object[i]['name'],
value = object[i]['value'];
if( IsPrimaryDataType( value )) {
returnObj[ key ] = value;
}
else {
convertToFirstLevelObject( value );
}
}
else{
/* This condition is for Array of Array */
if( object[i] instanceof Array ) {
convertToFirstLevelObject( object[i] );
} else {
console.log('Wrong data passed, expected data id object or Array of objects');
return;
}
}
}
}
else {
for ( var attr in object ) {
if ( object.hasOwnProperty( attr ) ) {
if( IsPrimaryDataType( object[ attr ] )) {
returnObj[ attr ] = object[ attr ];
}
else {
convertToFirstLevelObject( object[ attr ] )
}
}
}
}
}
return returnObj;
}
function IsPrimaryDataType( input ) {
var returnFlag = false;
if( input === null || input === 'undefined' || typeof input !== 'object' ) {
returnFlag = true;
}
return returnFlag;
}
Edit:
Here is another inputObj
just to show that nesting can be of any level, here I have increased the level of array nesting. In any level of array nesting it will just look for if there is any object which has name
and value
both the property then it will flatten that.
var inputObj = {
'a' : 1,
'b' : true,
'c' : 'string1',
'd' : {
'e' : 'string2',
'f' : false,
'g' : 5,
'h' : {
'i' : 7,
'j' : 'string3',
'k' : [
{
'name' : 'l',
'value': 11
},
{
'name' : 'm',
'value': [{'n' : 13},{'o' : 'string4'}]
}
]
}
},
'p' : [
{
'name' : 'q',
'value': 15
},
{
'name' : 'r',
'value': 'Awesome!'
}
],
's' : [
[{
'name' : 't',
'value': 17
},
{
'name' : 'u',
'value': 'string5'
}],
[ 1, 2, 3],
[ "string6", "string7", "string8"],
[
[1,3,5],
[{'name' : 'v', 'value' : 19, 'anyOtherProp' : false}],
[2,4,6],
[{'name' : 'w', 'otherProp' : 31}]
]
]
}
OutObj should be like below
var outputObj = {
'a' : 1,
'b' : true,
'c' : 'string1',
'e' : 'string2',
'f' : false,
'g' : 5,
'i' : 7,
'j' : 'string3',
'l' : 11,
'n' : 13,
'o' : 'string4',
'q' : 15,
'r' : 'Awesome!',
't' : 17,
'u' : 'string5',
'v' : 19
}