Uniones de datos de matriz de objetos de Node.js

Are there any libraries or efficient techniques to perform array joins in node JS such that,

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?

efficiency is paramount here, since it could be handling huge arrays in production

preguntado el 31 de julio de 12 a las 15:07

1 Respuestas

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

¿Es eso lo que estás buscando?

Respondido 31 Jul 12, 16:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.