1

I have an array of objects:

objArray = [
    {x: 1, y: 7},
    {x: 2, y: 14},
    {x: 1, z: 9},
    {x: 2, z: 18}
    {x: 1, n: 6}
    {x: 2, n: 16}
]

Is there an efficient way to merge for "X" without a for loop? so that I end up with:

objArray = [
    {x: 1, y: 7, z: 9, n: 6},
    {x: 2, y: 14, z: 18, n: 16}
]

So look for common objArray[n]["x"] and merge all hits into one object? It's OK to modify the original array or create a new one.

I'm aware this can be done with a loop, but I'm trying to avoid too many loops for this implementation, though I'm not sure if a reduce or a filter would work for this.

4
  • 1
    What if the y value is the same in two different objects? Commented Dec 7, 2018 at 16:23
  • Should have specified that. It never will be for this use case. Only "X" will be the same. Commented Dec 7, 2018 at 16:23
  • 1
    Just curious, why do you want to avoid using a loop? Loops generally have better performance than the iteration methods from Array.prototype. The latter make for a more readable code, and performance is not an issue on small data samples, but for large data samples, a for loop performs significantly better. Commented Dec 7, 2018 at 17:10
  • I thought the opposite was true! I'm mainly looking for this to be fast, as the object arrays can be 700+ long, or even more. I was under the impression the array methods were actually faster! Commented Dec 7, 2018 at 17:15

3 Answers 3

3

You could take a Map and group by property x.

var array = [{ x: 1, y: 7 }, { x: 2, y: 14 }, { x: 1, z: 9 }, { x: 2, z: 18 }, { x: 1, n: 6 }, { x: 2, n: 16 }],
    result = Array.from(
        array
            .reduce((m, o) => m.set(o.x, Object.assign({}, m.get(o.x), o)), new Map)
            .values()
    );
    
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for your help, this works perfectly
3

You could use reduce method to build an object and then Object.values to get an array.

const data = [{"x":1,"y":7},{"x":2,"y":14},{"x":1,"z":9},{"x":2,"z":18},{"x":1,"n":6},{"x":2,"n":16}]
const res = data.reduce((r, {x, ...rest}) => {
  if(!r[x]) r[x] = {x, ...rest}
  else Object.assign(r[x], rest);
  return r;
}, {})

const result = Object.values(res);

console.log(result)

Comments

3

You can do it with Array#reduce:

const objArray = [
    {x: 1, y: 7},
    {x: 2, y: 14},
    {x: 1, z: 9},
    {x: 2, z: 18},
    {x: 1, n: 6},
    {x: 2, n: 16},
]

const result = Object.values( objArray.reduce( 
  (p,c) => (p[c.x] = Object.assign( {}, p[c.x], c ), p ), {}
) );

console.log( result );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.