hopefully there is someone here with enough experience to guide me through this particular issue I've found. So I am working with this peculiar API that returns an object akin to the following:
const api_response = {
environment_list: ["dev", "non-prod"],
member_list: [
{ id: "abc-456", name: "Homer", "last-name": "Simpson", role: "admin" },
],
};
The goal I'm trying to achieve is to create a new list that contains the user id, name, role, and environment based on the environment list above so it should look like this:
[
{ id: 'abc-456', name: 'Homer', role: 'admin', environment: 'dev' },
{ id: 'abc-456', name: 'Homer', role: 'admin', environment: 'non-prod' }
]
Now what I have done is the following:
I created a variable to isolate each value that will be be merged:
const userList = _.map(api_response.member_list, (data) => ({
id: data.id,
name: data.name,
role: data.role,
}));
const environmentList = _.map(api_response.environment_list, (data) => ({
environment: data,
}));
Then these two were merged as one list of objects:
const extendAndMergeList = _.map(environmentList, (data) =>
_.map(userList, (items) => ({ ...items, environment: data.environment }))
);
And the output is close enough to my final product which is:
[
[
{ id: 'abc-456', name: 'Homer', role: 'admin', environment: 'dev' }
],
[
{
id: 'abc-456',
name: 'Homer',
role: 'admin',
environment: 'non-prod'
}
]
]
I'm probably doing something silly but would love to have some guidance in how to trim this from a list of lists to a list of objects.
another look at all the steps I took:
const _ = require("lodash");
const api_response = {
environment_list: ["dev", "non-prod"],
member_list: [
{ id: "abc-456", name: "Homer", "last-name": "Simpson", role: "admin" },
],
};
const userList = _.map(api_response.member_list, (data) => ({
id: data.id,
name: data.name,
role: data.role,
}));
const environmentList = _.map(api_response.environment_list, (data) => ({
environment: data,
}));
const extendAndMergeList = _.map(environmentList, (data) =>
_.map(userList, (items) => ({ ...items, environment: data.environment }))
);
Thank you again for any helpful response!