E
E
ennet2015-09-25 15:20:53
JavaScript
ennet, 2015-09-25 15:20:53

How to find identical elements (objects) of 2 arrays?

There are 2 arrays of different lengths, whose elements are objects. It would be desirable to receive result in the form of an array, with these elements. Here are 2 arrays:

var arr1 = [{timeFormat: "00:00", timestamp: 1443128400000}, {timeFormat: "00:15", timestamp: 1443129300000}, {timeFormat: "02:15", timestamp: 1443136500000}];
var arr2 = [{timeFormat: "00:00", timestamp: 1443128400000}, {timeFormat: "01:00", timestamp: 1443132000000}, {timeFormat: "02:15", timestamp: 1443136500000}];


jsfiddle.net/yq0or6ct/4 here I tried to compare by index, it finds everything on a regular array, but when objects are in the form of elements - no. Tell me how to do it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Inchin ☢, 2015-09-25
@In4in

Well, something like this:

[].push.apply(arr1, arr2);
console.log(arr1);

Or into a new array:
Array.prototype.merger = function(){
  var arr = this.slice();
  this.forEach.call(arguments, function(e){
      arr.push.apply(arr, e);
  }); 
  return arr;
}

//Пример
arr1.merger(arr2, arr3, arr4); 
//Новый массив, содержащий элементы arr1-4

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question