Home > Enterprise >  Manipulating nested objects
Manipulating nested objects

Time:11-20

I have the following data structure:

{
    "nodes": [
        {
            "frontmatter": {
                "excerpt": null,
                "featured": true,
                "title": "A Post with Content"
            },
            "fields": {
                "slug": "posts/a-post-of-type-page",
            }
        },
        {
            "frontmatter": {
                "excerpt": null,
                "featured": null,
                "title": "A post of type post"
            },
            "fields": {

                "slug": "posts/a-post-of-type-post",
            }
        },
        {
            "frontmatter": {
                "excerpt": null,
                "featured": null,
                "title": "Another Post (or type post)"
            },
            "fields": {

                "slug": "posts/another-post-or-type-post",
            }
        },
        {
            "frontmatter": {
                "excerpt": "This is the excerpt of a post",
                "featured": null,
                "title": "With Content"
            },
            "fields": {

                "slug": "posts/with-content",
            }
        },
    ]
}

I know that I can use myObject.nodes.map(x => x.frontmatter) to bring the frontmatter up a level and removing the nesting. But, I now need to change each node into the following structure within the resulting array:

{
   "nodes": [
       {
           "excerpt": null,
           "featured": true,
           "title": "A Post with Content"
           "slug": "posts/a-post-of-type-page",
       },
      ...
   ]
}

So, I need to remove the nesting for both the frontmatter and fields.

Thanks

CodePudding user response:

const d = {"nodes":[{"frontmatter":{"excerpt":null,"featured":true,"title":"A Post with Content"},"fields":{"slug":"posts/a-post-of-type-page"}},{"frontmatter":{"excerpt":null,"featured":null,"title":"A post of type post"},"fields":{"slug":"posts/a-post-of-type-post"}},{"frontmatter":{"excerpt":null,"featured":null,"title":"Another Post (or type post)"},"fields":{"slug":"posts/another-post-or-type-post"}},{"frontmatter":{"excerpt":"This is the excerpt of a post","featured":null,"title":"With Content"},"fields":{"slug":"posts/with-content"}}]};

d.nodes = d.nodes.map(({frontmatter, fields})=>({...frontmatter, ...fields}));
console.log(d);

CodePudding user response:

Here is another solution:

const data = {"nodes":[{"frontmatter":{"excerpt":null,"featured":true,"title":"A Post with Content"},"fields":{"slug":"posts/a-post-of-type-page"}},{"frontmatter":{"excerpt":null,"featured":null,"title":"A post of type post"},"fields":{"slug":"posts/a-post-of-type-post"}},{"frontmatter":{"excerpt":null,"featured":null,"title":"Another Post (or type post)"},"fields":{"slug":"posts/another-post-or-type-post"}},{"frontmatter":{"excerpt":"This is the excerpt of a post","featured":null,"title":"With Content"},"fields":{"slug":"posts/with-content"}}]};

let newArr = [];
data.nodes.forEach((n)=>newArr.push(Object.assign(n.fields, n.frontmatter)));
console.log({"nodes": newArr});

  • Related