Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
  };

>Solution :

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]] : []})));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading