简体   繁体   中英

How to join with two associative arrays using an ID in JavaScript?

I have two arrays and what I need to do is combine them and give me the result array. However I have tried everything I could and still have not been able to, I was trying with the information on this link but still it has not given me

https://stackoverflow.com/questions/42429023/how-can-i-perform-an-inner-join-with-two-object-arrays-in-javascript# =

const ap_procedures = [ 
{num_document: "39553466"}, 
{num_document: "39559282"}, 
{num_document: "39553466"}, 
];

const us_users = [ 
{num_document: "39553466", errors: "lorem ipsun #1"}, 
{num_document: "39559282", errors: "lorem ipsun #2"}, 
];

const result = [ 
{num_document: "39553466", errors: "lorem ipsun #1"}, 
{num_document: "39559282", errors: "lorem ipsun #2"}, 
{num_document: "39553466", errors: "lorem ipsun #1"}, 
];

The easiest thing to do would be to make an object out of the us_users array with reduce , then map the ap_procedures array:

 const ap_procedures = [{num_document:"39553466"},{num_document:"39559282"},{num_document:"39553466"}]; const us_users = [{num_document:"39553466",errors:"lorem ipsun #1"},{num_document:"39559282",errors:"lorem ipsun #2"}]; const obj = us_users.reduce((a, { num_document, errors }) => ({...a, [num_document]: errors }), {}); const result = ap_procedures.map(({ num_document }) => ({ num_document, errors: obj[num_document] })); console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; auto; }

The straightforward way is to loop through ap_procedures and for each item search for a matching item in us_users :

let result = ap_procedures.map(item => {
    let key = item.num_document;

    return us_users.find(x => x.num_document === key);
});

This works and is fairly easy to understand. If you are not familiar with map() and find() the code above is basically doing this:

let result = [];

for (let i=0; i<ap_procedures.length; i++) {
    let item = ap_procedures[i];
    let key = item.num_document;

    for (let j=0; j<us_users.length; j++) {
        if (us_users[j].num_document === key) {
            result.push(us_users[j]);
        }
    }
}

However this has a time complexity of O(n-ap_procedures) * O(n-us_users) . A faster technique is to build a map/dictionary of us_users items by the key you're joining with then use that map instead. This is assuming that us_users is unique:

// build the map
let map = {};
us_users.forEach(x => map[x.num_document] = x);

// use the map
let result = ap_procedures.map(item => {
    let key = item.num_document;

    return map[key];
});

This runs in O(n-ap_procedures) + O(n-us_users) .

Why not use a hashmap?

const resultObj = {}

eachArray.reduce((accum, el) => ({...accum, [el.num_documento]: el}), resultObj)

Then you can map over Object.keys(resultObj) to make a new array...

Note: this overwrites with the final object of each duplicate. You can destructure in the old value, but you will overwrite any previous values for that num_documento. You can solve this too...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM