简体   繁体   English

Javascript组合两个对象数组

[英]Javascript combine two array of objects

I would like to combine elements of 2 arrays based on the name.我想根据名称组合 2 个数组的元素。 For example:例如:

Array1 = [
  {name: "name1", language: "lang1"}, 
  {name: "name2", language: "lang2"}, 
  {name: "name3", language: "lang3"}]

Array2 = [
  {name: "name1", subject: "sub1"}, 
  {name: "name2", subject: "sub2"}, 
  {name: "name3", subject: "sub3"}]

I need to generate the following array:我需要生成以下数组:

Array3 = [
  {language: "lang1", subject: "sub1"}, 
  {language: "lang2", subject: "sub2"}, 
  {language: "lang3", subject: "sub3"}]

The logic I could think of was to write an explicit for loop to compare every element of first array with every element of second array and check if name matches as shown below.我能想到的逻辑是编写一个显式的 for 循环,将第一个数组的每个元素与第二个数组的每个元素进行比较,并检查名称是否匹配,如下所示。

let Array3 = []
for(let i=0;i<Array1.length;i++)
{
    let elem = Array1[i];
    for(let j=0;j<Array2.length;j++)
    {
        if(Array2[j].name===elem.name)
        {
            Array3.append({language: elem.language, subject: Array2[j].subject})
            break;
        }
    }
}

However, my actual dataset is quite large and this seems inefficient.但是,我的实际数据集非常大,这似乎效率低下。 How can this can be achieved in a more efficient manner (like using higher order functions or something)?如何以更有效的方式实现这一点(例如使用高阶函数或其他东西)?

You need to iterate over the two arrays and group the generated object in a map having the name as the key :您需要遍历两个数组并将生成的对象分组到一个以名称为key的映射中:

 let Array1 = [ {name: "name1", language: "lang1"}, {name: "name2", language: "lang2"}, {name: "name3", language: "lang3"} ]; let Array2 = [ {name: "name1", subject: "sub1"}, {name: "name2", subject: "sub2"}, {name: "name3", subject: "sub3"} ]; let map = new Map(); Array1.forEach(e => map.set(e.name, {language: e.language})); Array2.forEach(e => { if(map.has(e.name)) map.set(e.name, {...map.get(e.name), subject: e.subject}); }); let Array3 = [...map.values()].filter(e => e.language && e.subject); console.log(Array3);

Using a Map for O(1) lookup of one of the arrays using name as key lets you iterate each array only once.使用 Map for O(1) 查找使用名称作为键的数组之一可以让您只迭代每个数组一次。

 const Array1=[{name:"name1",language:"lang1"},{name:"name2",language:"lang2"},{name:"name3",language:"lang3"}],Array2=[{name:"name1",subject:"sub1"},{name:"name2",subject:"sub2"},{name:"name3",subject:"sub3"}]; const a1Map = new Map(Array1.map(({name, ...r})=> [name, {...r}])); const res = Array2.map(({name, ...r}) => ({...r, ...a1Map.get(name)})) console.log(res)

Yes you are thinking in right order , you need to use the sort algorithm logics , I will say nested for loops will be just as good.是的,您按正确的顺序思考,您需要使用排序算法逻辑,我会说嵌套 for 循环也一样好。 With larger dataset , since you need to extract the values from two different array you can use the nested for loops.对于较大的 dataset ,由于您需要从两个不同的数组中提取值,因此可以使用嵌套的 for 循环。

 for(int i=0;i>array1.length();i++){
  This can be use for first array
  Define String x=",";
  For second
  for(int j=0;j>array2.length();j++)
  {
    Check if ( (","+j+",").contains(x)) then break;
    If array1 name found in array 2, store array3 as you want 
    Also Store value of j in x
    Like x=x +j+",";
   }}

This way your nested for loop will skip the comparison code.这样您的嵌套 for 循环将跳过比较代码。 Above algo is raw but will reduce the complexity a significant bit.以上算法是原始的,但会显着降低复杂性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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