JavaScript Merging two array of objects and its properties merge array objects javascript,merge array objects lodash,merge array

I have two arrays which I want to merge.

var arrayOne = [{id:1},{id:2}];
var arrayTwo = [{age: 20}, {age: 25}, {age: 22}];

Merged array should be like this

[{id:1, age: 20}, {id:2, age: 25}, {age: 22}]

I have tried _.zip method from underscore library but it only merges array and not array of objects.

Thanks

Answer:1

something more general:

function merge(...arrays){
  var length = arrays.reduce((len, arr) => Math.max(len, arr.length), 0);
  var assign = i => (into, arr) => Object.assign(into, arr[i]);
  return Array.from({ length }, (v,i) => arrays.reduce(assign(i), {}));
}

var arrayOne = [{id:1},{id:2}];
var arrayTwo = [{age: 20}, {age: 25}, {age: 22}];

merge(arrayOne, arrayTwo /*or as many as you want*/ );
Answer:2

An ES6 approach:

const mergeArrays = (arr1, arr2) => {
    if (arr2.length > arr1.length) [arr1,arr2] = [arr2, arr1] //swap: larger first
    return arr1.map( (item, idx) => Object.assign({}, item, arr2[idx]) )
}

mergeArrays(
    [{id:1},{id:2}], [{age: 20}, 
    {age: 25}, {age: 22}]
)
//[ { age: 20, id: 1 }, { age: 25, id: 2 }, { age: 22 } ]

mergeArrays(
    [{age: 20}, {age: 25}, {age: 22}],
    [{id:1},{id:2}]
)
//[ { age: 20, id: 1 }, { age: 25, id: 2 }, { age: 22 } ]

Note that length can be different and order doesn't matter (you can pass the bigger array after or before, the result is the same).

Answer:3
function zipObjects() {
    return _.zip.apply(_, arguments).map(
        function(arr) {
            return _.extend.apply(_, [{}].concat(arr));
        }
    );
}

or in ES6:

function zipObjects(...args) {
    return _.zip(...args).map(
        (arr) => _.extend({}, ...arr)
    );
}
Answer:4

Good use case for a generator with object spread props:

function* zip(...args) {
  const len = Math.max(...args.map(a => a.length));
  for (let i = 0; i < len; i++) {
      yield Object.assign({}, ...args.map(a => a[i]));
  }
}

This will yield an iterator, not an array, so you can get an array result with a simple Array.from or an array spread, like so

var arrayOne = [{id:1},{id:2}];
var arrayTwo = [{age: 20}, {age: 25}, {age: 22}];

console.log([...zip(arrayTwo, arrayOne)]);

edit: if you don't love yielding, you can map from a new array built to the longest length. Works with any ordering of arrays, and any number of arrays of objects.

const zipAr = (...args) => Array
.from({ length: Math.max(...args.map(a => a.length)) })
.map((_, i) => Object.assign({}, ...args.map(a => a[i])));
Answer:5

Object.assign() would be a key method for merging objects..

Example:

var arrayOne = [{id:1},{id:2}];
var arrayTwo = [{age: 20}, {age: 25}, {age: 22}];


//Lets assume that we want to update arrayTwo ..

for(var i = 0 ; i < arrayOne.length; i++){
   arrayTwo[i] = Object.assign(arrayTwo[i] , arrayOne[i])
}


console.log(arrayTwo);

Hope this helps..

Answer:6

Try this one liner code using array.map() & Object.assign() method .

DEMO

var arrayOne = [{id:1},{id:2}];
var arrayTwo = [{age: 20}, {age: 25}, {age: 22}];

var res = arrayTwo.map(function(item, index) { return Object.assign(item, arrayOne[index]);
});

console.log(res);
Answer:7

I am creating a self scrolling, auto updating table and need to search through through elements of a class from a certain point. My code currently is this: var nextScroll; setInterval(function() { ...

I am creating a self scrolling, auto updating table and need to search through through elements of a class from a certain point. My code currently is this: var nextScroll; setInterval(function() { ...

  1. jquery loop through array
  2. jquery loop through elements
  3. jquery loop through json
  4. jquery loop through table rows
  5. jquery loop through elements by class
  6. jquery loop through select options
  7. jquery loop through json array
  8. jquery loop through child elements
  9. jquery loop through checkboxes
  10. jquery loop through json object
  11. jquery loop through form elements
  12. jquery loop through array of objects
  13. jquery loop through list
  14. jquery loop through dictionary
  15. jquery loop through table
  16. jquery loop through checked checkboxes
  17. jquery loop through inputs
  18. jquery loop through elements by name
  19. jquery loop through td in tr
  20. jquery loop through divs

How can I get the value of input on click using vanilla javascript? function getvalue() { console.log(this.val); } <input type="text" onclick="getvalue()" value="asdf"></input> &...

How can I get the value of input on click using vanilla javascript? function getvalue() { console.log(this.val); } <input type="text" onclick="getvalue()" value="asdf"></input> &...

  1. value from input javascript
  2. value from input field javascript
  3. value from input angular
  4. value from input jquery
  5. value from input
  6. value from input php
  7. value from input html
  8. value from input field
  9. value from input text
  10. value from input text javascript
  11. value from input type text
  12. value from input radio
  13. value from input java
  14. value from input box
  15. get value from input javascript
  16. get value from input jquery
  17. get value from input
  18. get value from input js
  19. get value from input react
  20. get value from input angular

I tried loading the events dynamically but did not get the output. I brought data using AJAX in the format of var datalist = "2015-09-22":{} $(".responsive-calendar").responsiveCalendar({ events:...

I tried loading the events dynamically but did not get the output. I brought data using AJAX in the format of var datalist = "2015-09-22":{} $(".responsive-calendar").responsiveCalendar({ events:...

I am trying to catch keycode for Dot and Delete but on different events. function validateSomething(eve) { var charCode = (eve.which) ? eve.which : eve.keyCode if (charCode == 46) console.log("...

I am trying to catch keycode for Dot and Delete but on different events. function validateSomething(eve) { var charCode = (eve.which) ? eve.which : eve.keyCode if (charCode == 46) console.log("...