3

suppose i have this data:

data = [{
    "_id" : "2fApaxgiPx38kpDLA",
    "profile" : {
        "name" : "Karina 1",
        "avatar" : "avatar1.jpg",
        "bio" : "my bio 1"
},
{
    "_id" : "NXM6H4EWfeRAAhB7c",
    "profile" : {
        "name" : "Karina 2",
        "avatar" : "avatar2.jpg",
        "bio" : "my bio 2"
    },
}];

i did _.map(data, "profile") but it remove top level _id:

wrongResult = [{
    "name" : "Karina 1",
    "avatar" : "avatar1.jpg",
    "bio" : "my bio 1"
},
{
    "name" : "Karina 2",
    "avatar" : "avatar2.jpg",
    "bio" : "my bio 2"  
}];

How to move nested object into top level without removing top level like this one:

expectedResult = [{
    "_id" : "2fApaxgiPx38kpDLA",
    "name" : "Karina 1",
    "avatar" : "avatar1.jpg",
    "bio" : "my bio 1"
},
{
    "_id" : "NXM6H4EWfeRAAhB7c",
    "name" : "Karina 2",
    "avatar" : "avatar2.jpg",
    "bio" : "my bio 2"  
}];

thank You so much....

Julian
  • 4,176
  • 19
  • 40
  • Duplicate of https://stackoverflow.com/q/33036487/1166087. I gave a single-line solution here: https://stackoverflow.com/a/70385062/1166087. – Julian Jan 05 '22 at 19:18
  • Does this answer your question? [One liner to flatten nested object](https://stackoverflow.com/questions/33036487/one-liner-to-flatten-nested-object) – Julian Jan 05 '22 at 19:20

4 Answers4

3

Something Like this? (not tested)

_.map(data,function(d){
    d.profile._id = d._id;
    return d.profile;
});
perusopersonale
  • 896
  • 1
  • 8
  • 18
2

Recently needed to do something like this myself. Ended up writing a general purpose function to bring all (nested included) object values to the top level:

const reduceObjValues = (obj, cache = {}) => {
    const objectValues = Object.keys(obj).reduce((acc, cur) => {
        if (!Array.isArray(obj[cur]) && typeof obj[cur] === 'object') {
            return reduceObjValues({ ...acc, ...obj[cur] }, cache);
        }
        acc[cur] = obj[cur];

        return acc;
    }, {});

    return {
        ...objectValues,
        ...cache,
    };
}
reduceObjValues({
  a: {
    b: 'a',
    c: 'b',
  },
  d: {
    e: 'a',
    f: {
      g: {
        h: [
          1,
          2,
          3,
        ]
      }
    }
  }
});
=> { b: 'a', c: 'b', e: 'a', h: [ 1, 2, 3 ] }

one issue with this function is that it will overwrite any keys that are the same.

Marty
  • 36
  • 4
0

You can use flatten to move the nested object to its parent level... https://www.npmjs.com/package/flat

VShank
  • 41
  • 6
0

Since you are using lodash, I came up with a generic function to flatten out any deeply nested object.

const flattener = obj => {
        const toPairs = obj => _.entries(obj).map(([key, val]) => typeof val === 'object' ? toPairs(val) : [key, val]);
        return _.chain(toPairs(obj)).flattenDeep().chunk(2).fromPairs().value();
    }

So, with an array like this

data = [
{
    "_id" : "2fApaxgiPx38kpDLA",
    "profile" : {
        "name" : "Karina 1",
        "avatar" : "avatar1.jpg",
        "bio" : "my bio 1"
    }
},
{
    "_id" : "NXM6H4EWfeRAAhB7c",
    "profile" : {
        "name" : "Karina 2",
        "avatar" : "avatar2.jpg",
        "bio" : "my bio 2"
    },
}
]

you can do

data.map(obj => flattener(obj))

which will give you

[
    {
        "_id": "2fApaxgiPx38kpDLA",
        "name": "Karina 1",
        "avatar": "avatar1.jpg",
        "bio": "my bio 1"
    },
    {
        "_id": "NXM6H4EWfeRAAhB7c",
        "name": "Karina 2",
        "avatar": "avatar2.jpg",
        "bio": "my bio 2"
    }
]

NB: This flattener function will throw away duplicate object keys, so if you have an object like;

myObj = { name: 'rick', age: 10, country: { name: 'uganda' } }

Flattening this out by calling flattener(myObj) will result in

{ name: 'uganda', age: 10 }

and not in

{ name: 'uganda', age: 10, name: 'rick' }

because you can't have an object with 2 similar keys even if the values to those keys are unique.

Richard
  • 412
  • 6
  • 9