Home > Blockchain >  How to merge these two arrays based on the id?
How to merge these two arrays based on the id?

Time:04-29

Im learning how to code and practicing, so I have this two functions

const getUsers = () => {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve([
                    { id: 1, name: 'Antonio', gender: 'male', status: 'active', salary: 1000 },
                    { id: 2, name: 'Rosa', gender: 'female', status: 'active', salary: 1000 },
                    { id: 3, name: 'Joseph', gender: 'male', status: 'inactive', salary: 2000 },
                    { id: 4, name: 'Lisa', gender: 'female', status: 'active', salary: 2000 },
                    { id: 5, name: 'Gwen', gender: 'female', status: 'inactive', salary: 3000 },
                    { id: 6, name: 'Antonio', gender: 'male', status: 'inactive', salary: 3000 }
                ]);
            }, 1000);
        });
    }
    
    const getCompanies = () => {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve([
                    { id: 1, name: 'Disney', employees: [1, 3], status: 'inactive' },
                    { id: 2, name: 'Nestle', employees: [4], status: 'active' },
                    { id: 3, name: 'Microsoft', employees: [2, 5, 6], status: 'active' }
                ]);
            }, 3000);
        });
    }

Based on the functions above, how can I create a function to get companies data filled with users data based on id from the employees key array?

response example:

[
    { id: 1...
    {
        id: 2,
        name: Nestle,
        employees: [
            {
                id: 4,
                name: 'Lisa',
                gender: 'female',
                status: 'active',
                salary: 2000
            }
        ],
        status: 'active'
    }
    { id: 3...
]

CodePudding user response:

You can create a helper function to merge the data.

The snippet below should work for you

const getUsers = () => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve([
        { id: 1, name: 'Antonio', gender: 'male', status: 'active', salary: 1000 },
        { id: 2, name: 'Rosa', gender: 'female', status: 'active', salary: 1000 },
        { id: 3, name: 'Joseph', gender: 'male', status: 'inactive', salary: 2000 },
        { id: 4, name: 'Lisa', gender: 'female', status: 'active', salary: 2000 },
        { id: 5, name: 'Gwen', gender: 'female', status: 'inactive', salary: 3000 },
        { id: 6, name: 'Antonio', gender: 'male', status: 'inactive', salary: 3000 },
      ]);
    }, 1000);
  });
};

const getCompanies = () => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve([
        { id: 1, name: 'Disney', employees: [1, 3], status: 'inactive' },
        { id: 2, name: 'Nestle', employees: [4], status: 'active' },
        { id: 3, name: 'Microsoft', employees: [2, 5, 6], status: 'active' },
      ]);
    }, 3000);
  });
};

// Merge and populate data
const mergeData = (users, companies) => {
  return companies.map((company) => {
    const employees = users.filter((user) => company.employees.includes(user.id));
    return { ...company, employees };
  });
};

(async () => {
  console.log('Running...');
  // Just a async context
  const users = await getUsers();
  const companies = await getCompanies();
  const mergedData = mergeData(users, companies);
  console.log(mergedData);
})();

CodePudding user response:

I would do it like that but there are for sure 1000 ways to do that

const employeesData = [
  { id: 1, name: "Antonio", gender: "male", status: "active", salary: 1000 },
  { id: 2, name: "Rosa", gender: "female", status: "active", salary: 1000 },
  { id: 3, name: "Joseph", gender: "male", status: "inactive", salary: 2000 },
  { id: 4, name: "Lisa", gender: "female", status: "active", salary: 2000 },
  { id: 5, name: "Gwen", gender: "female", status: "inactive", salary: 3000 },
  { id: 6, name: "Antonio", gender: "male", status: "inactive", salary: 3000 },
];

const companyData = [
  { id: 1, name: "Disney", employees: [1, 3], status: "inactive" },
  { id: 2, name: "Nestle", employees: [4], status: "active" },
  { id: 3, name: "Microsoft", employees: [2, 5, 6], status: "active" },
];

const fill = (employees, compenies) => {
  return compenies.map((company) => {
    company.employees = company.employees.map((id) => employees[id - 1]);
    return company;
  });
};

const merge = fill(employeesData, companyData);

console.log(JSON.stringify(merge, null, 2));

  • Related