JavaScript - merge two arrays of objects and de-duplicate based on property value

javascript merge two arrays of objects without duplicates es6
merge duplicate objects in array javascript
javascript compare two arrays of objects and remove duplicates
javascript merge array of objects by key
lodash merge array of objects without duplicates
remove duplicates from two array javascript
merge two arrays alternatively in javascript
merge two arrays as key value pairs javascript

I want to update (replace) the objects in my array with the objects in another array. Each object has the same structure. e.g.

var origArr = [
  {name: 'Trump', isRunning: true},
  {name: 'Cruz', isRunning: true},
  {name: 'Kasich', isRunning: true}
];
var updatingArr = [
  {name: 'Cruz', isRunning: false},
  {name: 'Kasich', isRunning: false}
];
// desired result:
NEWArr = [
  {name: 'Trump', isRunning: true},
  {name: 'Cruz', isRunning: false},
  {name: 'Kasich', isRunning: false}
];

I've tried concat() & Underscore's _.uniq function, but it always dumps the newer object & returns, essentially, the original array.

Is there a way to overwrite (replace) origArr with the objects in updatingArr -- matching on the name property?

Using a double for loop and splice you can do it like so:

for(var i = 0, l = origArr.length; i < l; i++) {
    for(var j = 0, ll = updatingArr.length; j < ll; j++) {
        if(origArr[i].name === updatingArr[j].name) {
            origArr.splice(i, 1, updatingArr[j]);
            break;
        }
    }
}

Example here

How to merge two arrays in JavaScript and de-duplicate items , splice(j--, 1); } } return a; }; Then, to use it: var array1 = ["Vijendra","Singh"]; var array2 = ["Singh", "Shakya"]; // Merges both arrays and gets unique items var array3 = array1. concat(array2). Here is a slightly different take on the loop. With some of the optimizations in the latest version of Chrome, it is the fastest method for resolving the union of the two arrays (Chrome 38.0.2111).

I came here looking for exactly this, saw @Gruff Bunny 's technique and wondered if 'lodash' wouldn't perhaps be a superior option even to 'underscore'?

Lo and behold :

let result = _.unionBy(updatingArr, origArr, 'name');

Removing duplicates from an array with vanilla JavaScript, How do you merge two arrays into one while also remove duplicates? To merge two arrays in JavaScript, use the Array.concat() method. JavaScript array concat() method returns a new array comprised of this array joined with two or more arrays. Example. You can try to run the following code to merge two arrays: Live Demo

You could use Array#map in combination with Array#reduce

var origArr = [{ name: 'Trump', isRunning: true }, { name: 'Cruz', isRunning: true }, { name: 'Kasich', isRunning: true }],
    updatingArr = [{ name: 'Cruz', isRunning: false }, { name: 'Kasich', isRunning: false }],
    NEWArr = origArr.map(function (a) {
        return this[a.name] || a;
    }, updatingArr.reduce(function (r, a) {
        r[a.name] = a;
        return r;
    }, Object.create(null)));

document.write('<pre>' + JSON.stringify(NEWArr, 0, 4) + '</pre>');

How to Combine Two Arrays in NodeJS, You have array of objects where some of them are duplicates but with different object properties, and you want to get only one object with all of those properties. Map and append to existing pair, if not creating new one with values provided. Build a simple Circle-Based Physics Simulation with pi2.js. Spreading an object allows for the addition of new properties and values to an object without mutations (i.e. a new object is created) and it can also be used to combine multiple objects together. It should be noted that spreading objects does not do nested copying. Example. Add a new object property and value without mutating the original object.

You can use a hash which gives the index by name, and Object.assign to update.

var hash = origArr.reduce(function(hash, obj, index) {
  hash[obj.name] = index;
  return hash;
}, Object.create(null));
for(var obj of updatingArr) {
  Object.assign(origArr[hash[obj.name]], obj);
}

Combining Arrays Without Duplicates ― Scotch.io, Next: Write a JavaScript function to remove a specific element from an array. What is the difficulty level of this exercise? Easy Medium Hard.   Definition and Usage. The concat() method is used to join two or more arrays.. This method does not change the existing arrays, but returns a new array, containing the values of the joined arrays.

You can give this a try.

var origArr = [
  {name: 'Trump', isRunning: true},
  {name: 'Cruz', isRunning: true},
  {name: 'Kasich', isRunning: true}
];
var updatingArr = [
  {name: 'Cruz', isRunning: false},
  {name: 'Kasich', isRunning: false}
];

var origLength = origArr.length;
var updatingLength = updatingArr.length;

//Traverse the original array and replace only if the second array also has the same value
for(i = origLength-1; i >= 0; i--) {
    for(j = updatingLength -1; j >= 0; j--) {
    if(origArr[i].name === updatingArr[j].name) {
        origArr[i] = updatingArr[j];
    }
  }
}

console.log(origArr);

JavaScript: Merge duplicate objects in array of objects, Merge two arrays in Javascript - The concat() method is used to join two or more How to merge two arrays in Javascript and de-duplicate items ? Merge properties of two Javascript objects writable: false, value: function() { var a = this.concat(); for(var i=0; i<a.length; Wikitechy Rated 5 / 5 based on 378284 reviews. Array#concat(..) is the tried and true approach for combining two (or more!) arrays. But the hidden danger is that it's creating a new array instead of modifying one of the existing ones. But the hidden danger is that it's creating a new array instead of modifying one of the existing ones.

JavaScript array: Merge two arrays and removes all duplicates , So how does this work when merging should happen using a property of the array object? Now we have two Arrays containing all the Id values of Objects. The first one has unique Ids and the second one has. duplicate Id 42342. To check for duplicates in Arrays, we can create a set from the array. Sets will only have the unique items in the array

[Solved-100% Working Code], Given an array of objects and the task is to remove the duplicate object element from the array list. There are two methods to solve this problem which are discussed below: Method 1: Anyone of the object properties can be used as a key. The key is How to get all unique values (remove duplicates) in a JavaScript array? You want to render this list, but first you want to order it by the value of one of the properties. For example you want to order it by the color name, in alphabetical order: black, red, white. You can use the sort() method of Array, which takes a callback function, which takes as parameters 2 objects contained in the array (which we call a and b):

Remove duplicates from js array (ES5/ES6) · GitHub, How to Merge Two Arrays in JavaScript and De-duplicate Items This version uses the concat() method to merge two arrays: The Set object is used to store unique values of primitive values or object references. How to Merge Properties of Two JavaScript Objects Dynamically · How to Remove an Element from an  JavaScript : find an object in array based on object's property (and learn about the "find" function) Published on March 20, 2017 March 20, 2017 • 310 Likes • 48 Comments Report this post

Comments
  • Is that what you are meaning? jsfiddle.net/4p98w3su not sure I understood the question.
  • All good answers here; I like yours the best, using splice. If you want to convert this to an Answer, I'll mark as Accepted. Thanks.
  • I put it as an answer, I think it is also faster than the methods involving map & reduce. I was getting about twice the speed when running it millions of times in a while loop.
  • traversal is done in the reverse order since it is least time consuming, according to tests conducted by jspref