How to merge two arrays in JavaScript and de-duplicate items
To just merge the arrays (without removing duplicates)
ES5 version use Array.concat
:
var array1 = ["Vijendra", "Singh"];
var array2 = ["Singh", "Shakya"];
array1 = array1.concat(array2);
console.log(array1);
JavaScript - merge two arrays of objects and de-duplicate based on property value
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
To just merge the arrays (without removing duplicates)
ES5 version use Array.concat
:
var array1 = ["Vijendra", "Singh"];
var array2 = ["Singh", "Shakya"];
array1 = array1.concat(array2);
console.log(array1);
Javascript: Merge Two Arrays of Objects, Only If Not Duplicate (Based on Specified Object Key)
You can create a set of IDs from initialData
and this will make "check if ID is already in initial data" faster - O(1):
var initialData = [{
'ID': 1,
'FirstName': 'Sally'
},
{
'ID': 2,
'FirstName': 'Jim'
},
{
'ID': 3,
'FirstName': 'Bob'
}
];
var newData = [{
'ID': 2,
'FirstName': 'Jim'
},
{
'ID': 4,
'FirstName': 'Tom'
},
{
'ID': 5,
'FirstName': 'George'
}
];
var ids = new Set(initialData.map(d => d.ID));
var merged = [...initialData, ...newData.filter(d => !ids.has(d.ID))];
console.log(merged);
Concat multiple arrays into one array without duplicates
var a = ["1","2","3"]
, b = ["3","4","5"]
, c = ["4","5","6"]
, d = [];
function newArray(x,y,z) {
x.concat(y,z).forEach(item =>{
if (d.indexOf(item) == -1)
d.push(item);
});
return d;
}
console.log(newArray(a,b,c));
JavaScript - Merge two array by key and keep duplicates
You could take an object for grouping same sku
and shift
objects for keeping the same order fro merging.
const
array1 = [{ sku: "35189424", price: 107800, isNew: false, name: "Product Title A" }, { sku: "62617802", price: 107800, isNew: false, name: "Product Title D" }, { sku: "GRP00437", price: 107800, isNew: false, name: "Product Title B" }, { sku: "62617802", price: 107800, isNew: false, name: "Product Title D" }, { sku: "35189432", price: 107800, isNew: false, name: "Product Title YZ" }],
array2 = [{ sku: "35189424", Url: "https://......", rating: 2, status: 0 }, { sku: "62617802", Url: "https://......", rating: 5, status: 1 }, { sku: "GRP00437", Url: "https://......", rating: 2, status: 1 }, { sku: "35189432", Url: "https://......", rating: 3, status: 1 }, { sku: "62617802", Url: "https://......", rating: 5, status: 1 }],
skus = array2.reduce((r, o) => ((r[o.sku] = r[o.sku] || []).push(o), r), { }),
result = array1.map(o => ({ ...o, ...skus[o.sku].shift() }));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Combine two arrays In one without duplicates
You can easily achieve the result using Set
and flat
.
const mapedTopicsArray = [
["javascript", "reactjs"],
["Java", "reactjs"],
];
const topicsArrayMergedWithoutDuplicates = [...new Set(mapedTopicsArray.flat())];
console.log(topicsArrayMergedWithoutDuplicates );
Related Topics
Understanding Unique Keys For Array Children in React.Js
What Does "This" Refer to in Arrow Functions in Es6
How to Use Unicode-Aware Regular Expressions in JavaScript
What Does 'Return' Keyword Mean Inside 'Foreach' Function
How to Get a Timestamp in JavaScript
Working With $Scope.$Emit and $Scope.$On
Check If a User Has Scrolled to the Bottom (Not Just the Window, But Any Element)
Does JavaScript Have a Method Like "Range()" to Generate a Range Within the Supplied Bounds
Abort Ajax Requests Using Jquery
How to Access Object Properties Containing Special Characters
How to Test For an Empty JavaScript Object
Why Would a JavaScript Variable Start With a Dollar Sign