Home > Mobile >  How to join "array" to a "parent array" with a common key. (javascript)
How to join "array" to a "parent array" with a common key. (javascript)

Time:11-26

Below are two arrays, user_array and company_array.

The user_list is grouped by vendor_id. I want to create bottom group_list array by adding a new user_list key to company_list.

I would like to know how to implement this one. Thank you in advance.

const company_list =
[
    {
        "vendor_id": "62d884697df5ad65745001a9",
        "vendor_name": "vender_name_1",
    },
{
        "vendor_id": "add8846dad5ad657450s01a",
        "vendor_name": "vender_name_2",
    },
{
        "vendor_id": "34d8846dad5add57450sss3",
        "vendor_name": "vender_name_3",
    },
]
const user_list =
{
    "62d884697df5ad65745001a9": [
        {
            "user_id": "0e9a3faf-4dcc-4681-a153-2f6c7acd161d",
            "user_name": "user_1",
        },
        {
            "user_id": "f39769cb-e567-4da9-8e2c-9e39daaba9ed",
           "user_name": "user_2",
        }
    ],
    "add8846dad5ad657450s01a": [
        {
            "user_id": "de9adfaf-4dcc-d681-ad53-2f6cdacd161d",
            "user_name": "user_3",
        },
        {
            "user_id": "g397g9cb-e5g7-4dag-8e2cgge39daaba9",
           "user_name": "user_4",
        }
]
"34d8846dad5add57450sss3": [
        {
            "user_id": "deaadfaf-4dac-d6a1-ad5a-2f6adacd161a",
            "user_name": "user_5",
        },
        {
            "user_id": "g397b9cb-e5g7-4dab-8b2cgge39daaba9",
           "user_name": "user_6",
        }
]
}
const group_list =
[
    {
        "vendor_id": "62d884697df5ad65745001a9",
        "vendor_name": "vender_name_1",
       
        “user_list” : [
            {
                "user_id": "0e9a3faf-4dcc-4681-a153-2f6c7acd161d",
                "user_name": "user_1",
            },
            {
                "user_id": "f39769cb-e567-4da9-8e2c-9e39daaba9ed",
               "user_name": "user_2",
            },
        ],
    },
    {
        "vendor_id": "add8846dad5ad657450s01a",
        "vendor_name": "vender_name_2",
        “user_list” : [
            {
                "user_id": "de9adfaf-4dcc-d681-ad53-2f6cdacd161d",
                "user_name": "user_3",
            },
            {
                "user_id": "g397g9cb-e5g7-4dag-8e2cgge39daaba9",
               "user_name": "user_4",
            },
        ],
    },
    {
        "vendor_id": "34d8846dad5add57450sss3",
        "vendor_name": "vender_name_3",
        “user_list” : [
            {
                "user_id": "deaadfaf-4dac-d6a1-ad5a-2f6adacd161a",
                "user_name": "user_5",
            },
            {
                "user_id": "g397b9cb-e5g7-4dab-8b2cgge39daaba9",
               "user_name": "user_6",
            },
        ],
     },
]

The grouping of user_list was achieved by implementing the following But, it is stuck at the point of merging user_list into company_list.

const groupByCompany = async (array, key) => {
    const response = await array.reduce((result, currentValue) => {
      (result[currentValue[key]] = result[currentValue[key]] || []).push(
        currentValue
      );
      return result;
    }, {});
    return response;
  };

CodePudding user response:

You already have everything set up, just iterate through company list and get corresponding value from user_list object by a vendor_id.

const company_list = [ { "vendor_id": "62d884697df5ad65745001a9", "vendor_name": "vender_name_1", }, { "vendor_id": "add8846da45d5ad657450s01a", "vendor_name": "vender_name_2", }, { "vendor_id": "34d8846dad5add57450sss3", "vendor_name": "vender_name_3", }, ]; 
const user_list = { "62d884697df5ad65745001a9": [ { "user_id": "0e9a3faf-4dcc-4681-a153-2f6c7acd161d", "user_name": "user_1", }, { "user_id": "f39769cb-e567-4da9-8e2c-9e39daaba9ed", "user_name": "user_2", } ], "add8846dad5ad657450s01a": [ { "user_id": "de9adfaf-4dcc-d681-ad53-2f6cdacd161d", "user_name": "user_3", }, { "user_id": "g397g9cb-e5g7-4dag-8e2cgge39daaba9", "user_name": "user_4", } ], "34d8846dad5add57450sss3": [ { "user_id": "deaadfaf-4dac-d6a1-ad5a-2f6adacd161a", "user_name": "user_5", }, { "user_id": "g397b9cb-e5g7-4dab-8b2cgge39daaba9", "user_name": "user_6", } ] };


console.log(company_list.map(it => ({...it, user_list: user_list[it.vendor_id] ? [...user_list[it.vendor_id]] : []})));

CodePudding user response:

Go through each company in your array, find a corresponding user list based on vendor_id and user list's key.

Quick example on how that can be achieved:

const merged = company_list.map((company) => {
    const matchingUserListEntry = Object.entries(user_list)
      .find(([key]) => key === company.vendor_id)[1];
   
  if (!matchingUerListEntry) {
    // handle missing user list
  }

  return {
    ...company,
    user_list: matchingUserListEntry,
  }
});

console.log(merged);

CodePudding user response:

const list = Object.keys(user_list)
const array = company_list.map(item=>{
if(list.includes(item.vendor_id)){
item['user_list'] = user_list[item.vendor_id]
}
return item
})
  • Related