0

I have below array of objects with vertical as string and projects as array of objects inside it. projects can be empty too. Each projects entry if not empty, will have name and releaseType as properties.

[
  {
    "vertical": "Alpha",
    "projects": [
      {
        "name": "Test",
        "releaseType": ""
      }
    ]
  },
  {
    "vertical": "Beta",
    "projects": []
  },
  {
    "vertical": "Gamma",
    "projects": [
      {
        "name": "Lincoln",
        "releaseType": "Google Pay"
      },
      {
        "name": "Madison",
        "releaseType": "PayPal"
      }
    ]
  }
]

I want to get the list of projects in below format. Can someone please let me know how to achieve this. This isn't regular filter method, but it requires a bit of manipulation using map. I tried few things, but I was not able to achieve expected result.

[
  {
    "vertical": "Alpha",
    "feature": "Test",
     "releaseType": ""
  },
  {
    "vertical": "Gamma",
    "feature": "Lincoln",
     "releaseType": "Google Pay"
   },
   {
     "vertical": "Gamma",
     "feature": "Madison",
     "releaseType": "PayPal"
   }
]

4 Answers 4

3

You can use reduce for resolve it

arr.reduce((pre, cur) => {
  if (cur.projects.length) {
    pre.push(...cur.projects.map(p => ({
      vertical: cur.vertical,
      feature: p.name,
      releaseType: p.releaseType,
    })))
  }

  return pre;
}, []);
Sign up to request clarification or add additional context in comments.

Comments

1

Double flatMap, should solve this pretty well:

const input = [{vertical:"Alpha",projects:[{name:"Test",releaseType:""}]},{vertical:"Beta",projects:[]},{vertical:"Gamma",projects:[{name:"Lincoln",releaseType:"Google Pay"},{name:"Madison",releaseType:"PayPal"}]}];

const res = input.flatMap(e => 
  e.projects.flatMap(x => [{vertical: e.vertical, ...x}])
)

console.log(res)
.as-console-wrapper { max-height: 100% !important; top: 0; } /* ignore this */

Comments

1

You can use Array.reduce for this

let formattedProjects = arr.reduce((p, c, i, a) => {
    if (c.projects.length) {
        p = p.concat(c.projects.map(project => {
            return {
                vertical: c.vertical,
                feature: project.name,
                releaseType: project.releaseType
            }
        }));
    }

    return p;
}, []);

Demo: https://jsfiddle.net/6zk8jhat/

Comments

1
let arr = [
  {
    vertical: "Alpha",
    projects: [
      {
        name: "Test",
        releaseType: "",
      },
    ],
  },
  {
    vertical: "Beta",
    projects: [],
  },
  {
    vertical: "Gamma",
    projects: [
      {
        name: "Lincoln",
        releaseType: "Google Pay",
      },
      {
        name: "Madison",
        releaseType: "PayPal",
      },
    ],
  },
];

arr = arr
  .map((obj) => {
    return obj.projects.map((proj) => {
      return {
        vertical: obj.vertical,
        ...proj,
      };
    });
  })
  .flat();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.