按值合并Javascript中的两个多维数组

时间:2019-05-29 10:21:54

标签: javascript arrays multidimensional-array

我有两个数组:

var mergedarray = 
[['email1', object1, [['a1', 'b1']]],['email2', object2],['email3', object3, ['a3', 'b3']],['email4',object4,[['a4','b4'],['a4a','b4a']]]]

我需要通过电子邮件合并它们,这样结果数组将看起来像,没有重复的电子邮件:

{{1}}

看着这些多维维度,我的大脑很痛。有什么建议吗?

4 个答案:

答案 0 :(得分:3)

   const hash = new Map();

   for(const [email, ...rest] of [...array1, ...array2]) {
     if(hash.has(email)) {
       hash.get(email).push(...rest);
     } else {
        hash.set(email, [email, ...rest]);
    }
  }

  const result = [...hash.values()];

答案 1 :(得分:2)

创建array {2的Map,以加快搜索速度,并在第一个数组上使用Array.prototype.map,反之亦然。

var array1 = [['email1', 'object1'], ['email2', 'object2'], ['email3', 'object3'], ['email4', 'object4']]
var array2 = [['email1', [['a1', 'b1']]], ['email3', [['a3', 'b3']]], ['email4', [['a4', 'b4'], ['a4a', 'b4a']]]]

let map2 = new Map(array2);

let out = array1.map(([key, val]) => [key, val, map2.get(key) || []]);
console.log(out)

答案 2 :(得分:1)

您可以映射到第二个数组,并检查第一个元素是否在第一个数组的任何数组中,然后按第二个元素(如果返回真):

2019-05-29 11:31:29.116  INFO 4321 --- [           main] test.MyApplication  : EXECUTING : command line runner
2019-05-29 11:31:29.119  INFO 4321 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-05-29 11:31:29.124 ERROR 4321 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:816) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
        at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:324) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
        at com.jaletechs.png.PrimeNumberGeneratorApplication.main(MyApplication.java:19) [main/:na]
Caused by: java.util.NoSuchElementException: null
        at java.util.Scanner.throwFor(Scanner.java:862) ~[na:1.8.0_201]
        at java.util.Scanner.next(Scanner.java:1371) ~[na:1.8.0_201]
        at test.MyApplication.run(MyApplication.java:27) [main/:na]
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
        ... 3 common frames omitted

答案 3 :(得分:-1)

无法实施任何建议(可能是由于我的技能低下),我最终将数组变成对象以分配键值对,因此新数据如下:

var array1 = [{email:'email1', obj:object1, data:[]},{email:'email2', obj:object2, data:[]},{email:'email3', obj:object3, data:[]},{email:'email4',obj:object4, data:[]}];
var array2 = [{email:'email1',data:[{'a1','b1'}]},{email:'email3',data:[{'a3','b3'}]}, {email:'email4',data:[{'a4','b4'}]},{email:'email4',data:[{'a4a','b4a'}]}];

之后,我使用循环将对象数据从一个数组推入另一个数组:

 for (var x in array1) {
    var res = array1[x].email;

    for (var z in array2) {
      var cod = array2[z].email;
      var q = 0;
      if (res == cod) {
        array1[x].data.push(array2[z].data[0]);
        q++;
      }

    }
  }

结果是:

var array1 = [{email:'email1',obj:object1,data:[{'a1','b1'}]},{email:'email2',obj:object2,data:[]},{email:'email3',data:[{'a3','b3'}]},{email:'email4',obj:object4,data:[{'a4','b4'},{'a4a','b4a'}]}];