简体   繁体   English

Node.js对象数组数据联接

[英]Node.js object array data joins

Are there any libraries or efficient techniques to perform array joins in node JS such that, 是否有任何库或有效的技术来执行节点JS中的数组联接,从而,

A = [ { a: 1, b: 'a' }, { a: 2, b:'b' }, { a: 3, b: 'a' }, { a: 4, b: 'b' } ]
B = [ { a: 1, c: true }, { a: 2, c: true }, { a: 3, c: false } ]

could be joined such that the following results could be produced: 可以合并以产生以下结果:

# Intersection on a
C = [ { a: 1, b: 'a', c: true }, { a: 2, b: 'b', c: true }, { a: 3, b: 'a', c: false } ]

# Union on a
D = [ { a: 1, b: 'a', c: true }, { a: 2, b: 'b', c: true }, { a: 3, b: 'a', c: false }, { a: 4, b: 'b' } ]

Is array.map the best solution to this problem? 是array.map对此问题的最佳解决方案?

efficiency is paramount here, since it could be handling huge arrays in production 效率在这里至关重要,因为它可以处理生产中的大量阵列

You're not very specific about how you identify and merge your object. 您对如何识别和合并对象不是很明确。

Using Underscore, the result can be obtained as follow: 使用下划线,可以获得以下结果:

_u=require("underscore")
A = [ { a: 1, b: 'a' }, { a: 2, b:'b' }, { a: 3, b: 'a' }, { a: 4, b: 'b' } ]
B = [ { a: 1, c: true }, { a: 2, c: true }, { a: 3, c: false } ]

D = _u.zip(A,B).map( 
      function(x){ 
        return _u.extend(x[0],x[1]);
      } 
    );

C = _u.zip(A,B).filter(
      function(x){ 
        return !!x[1];
      }
    ).map(
      function(x){ 
        return _u.extend(x[0],x[1]);
      }
    );

Is that what you're looking for ? 那是您要找的东西吗?

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

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