0

Is there any method to merge 2 arrays of objects like this

var a = [{id: 1, val: 1},{id: 2, val: 2},{id: 3, val: 3},{id: 4, val: 4},{id: 5, val: 5}];
var b = [{id: 21, val: 21},{id: 22, val: 22},{id: 23, val: 23}]

//final result should be
c = [
  {id:1, val: 1},
  {id:21, val: 21},
  {id:2, val: 2},
  {id:22, val: 22},
  {id:3, val: 3},
  {id:23, val: 23},
  {id:4, val: 4},
  {id:5, val: 5}
]

offcourse I can create it by myself, but just want to check whether lodash provide it or not

2
  • Like card shuffling? One from this array and one from the other ...? Commented Mar 17, 2017 at 14:33
  • Yes correct...... Commented Mar 17, 2017 at 14:35

4 Answers 4

2

You could first zip the arrays, flatten the result and then use compact to remove the missing array elements (zip adds them as undefined):

var c = _.compact(_.flatten(_.zip(a,b)))

Or using chaining:

var c = _(a)
    .zip(b)
    .flatten()
    .compact()
    .value()

var a = [{id: 1, val: 1},{id: 2, val: 2},{id: 3, val: 3},{id: 4, val: 4},{id: 5, val: 5}];
var b = [{id: 21, val: 21},{id: 22, val: 22},{id: 23, val: 23}]

var c = _(a)
    .zip(b)
    .flatten()
    .compact()
    .value()
    
document.getElementById('results').textContent = JSON.stringify(c);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

<pre id="results"></pre>

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

Comments

1

var a = [{id: 1, val: 1},{id: 2, val: 2},{id: 3, val: 3},{id: 4, val: 4},{id: 5, val: 5}];
var b = [{id: 21, val: 21},{id: 22, val: 22},{id: 23, val: 23}];

// loop through the biggest array and reduce the result (no need for the value we just need the accumulator and the index)
var result = _.reduce(a.length < b.length? b: a, function(res, _, i) {
  if(i < a.length) res.push(a[i]); // if there is still elements in a, push the current one
  if(i < b.length) res.push(b[i]); // if there is still elements in b, push the current one
  return res;
}, []);

console.log(result);
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>

2 Comments

the more optimized approach would be to move length determining out of loop. Hint: See my approach aLen = a.length, bLen = b.length
@RomanPerekhrest I wanted this to be a one expression solution, otherwise I would've used _.forEach and push to an outer variable result and do all the tests outside!
1

In plain Javascript, you could use a function which iterates to the minimum length of both, assembles the values and concat the rest at the end.

function insert(a, b) {
    var c = [],
        i = 0,
        l = Math.min(a.length, b.length);
    while (i < l) {
        c.push(a[i], b[i]);
        i++;
    }
    return c.concat(a.slice(i), b.slice(i));
}

var a = [{ id: 1, val: 1 }, { id: 2, val: 2 }, { id: 3, val: 3 }, { id: 4, val: 4 }, { id: 5, val: 5 }],
    b = [{ id: 21, val: 21 }, { id: 22, val: 22 }, { id: 23, val: 23 }];

console.log(insert(a, b));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

1

Ecmascript5 solution using Math.max()(to find the larger array size) and Array.prototype.push() functions:

var a = [{id: 1, val: 1},{id: 2, val: 2},{id: 3, val: 3},{id: 4, val: 4},{id: 5, val: 5}],
    b = [{id: 21, val: 21},{id: 22, val: 22},{id: 23, val: 23}],
    maxLen = Math.max(a.length, b.length), aLen = a.length, bLen = b.length,
    maxList = aLen > bLen ? a : b;
    result = [];

for (var i = 0; i < maxLen; i++) {
    (i < aLen && i < bLen) ? result.push(a[i], b[i]) : result.push(maxList[i]);
}

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.