How to merge all arrays of an object array to get unique values?

javascript merge two arrays of objects without duplicates
javascript merge two arrays of objects without duplicates es6
merge duplicate objects in array javascript
javascript merge two objects without duplicates
merge two array of objects based on a key
javascript merge array of objects by key
javascript merge array of objects unique
js merge array of objects without duplicates

I've got some data like this:

const items = [
  { _id: '1', reference: ['abc'] },
  { _id: '2', reference: ['def'] },
  { _id: '3', reference: ['abc'] }

The length of items is always different.

Now I need to get all unique reference strings in a single array. So the result should be

['abc', 'def']

as 'abc' is a duplicate.

I tried to use a forEach() loop:

const references = []
items.forEach(i => {


But references just gets an empty array result. Also with that I did not take care of duplicates...

I would like to use an ES6 pattern. With that I know I could use something like this:

const array1 = ['abc']
const array2 = ['def']
const array3 = Array.from(new Set(array1.concat(array2)))

But how can I do this using a loop to get every reference array of each item object - even if I do not know how many objects are in the item array?

If you wanna do a functional style (map-reduce), you can do it like this

const items = [
  { _id: '1', reference: ['abc'] },
  { _id: '2', reference: ['def'] },
  { _id: '3', reference: ['abc'] }

// extract references and put them into a single array
const references = 
    .map(x => x.reference)
    .reduce((prev, cur) => prev.concat(cur))
// put them in a set to dedupe
const set = new Set(references)


Combining Arrays Without Duplicates ―, In modern javascript, its very easy to merge two objects, but do you know into an Array; Merging two arrays; Union of objects based on unique key What if both the object has same key, it simply merge the last objects value and have only It doesn't deep merge all the key values from both the array. Getting unique array values is another awesome usage of the spread operator. And don't forget you can merge object properties with the spread operator! There's no better feeling than being able to remove a library to complete a task that should be native to the language. This trick brings us one step closer to that!

If you want to use Sets, you can use Set.add, and spread syntax:

const items = [
  { _id: '1', reference: ['abc'] },
  { _id: '2', reference: ['def'] },
  { _id: '3', reference: ['abc'] }

var references = new Set();
items.forEach(i => references.add(...i.reference))

Union of objects in javaScript based on unique value, You can combine two arrays with distinct values using Union() 2, 3, 4, 3, 55, 23, 2 }; int[] num2 = { 55, 23, 45, 50, 80 }; var all = num1.Union(num2).ToArray();. Try it. If an array contains objects of a custom class, then you need to public string LastName { get; set; } public bool Equals(Person other) { return  This eliminates all duplicates as sets are designed to only hold unique values. We then spread the unique elements of the set back into an array which we return as the final result. Pretty simple, right? Another way to achieve this is by using the Array.from() method to convert the set which is an iterable object to an array as shown below:

This is similar to other solutions here, but creates a reusable function:

const uniqRefs = 
  items => [...items.reduce((s, i) => s.add(...i.reference), new Set())]

const items = [{"_id": "1", "reference": ["abc"]}, {"_id": "2", "reference": ["def"]}, {"_id": "3", "reference": ["abc"]}]

How to combine two arrays without duplicate values in C#?, In the first one, you create an array that contains two arrays… to get unique values out of, you can often simply combine the arrays and then cast to If I change the $master to a arraylist and try the .remove I still get an error? Spread the love Related Posts Cloning Arrays in JavaScriptThere are a few ways to clone an array in JavaScript, Object.assign Object.assign allows us… How to Merge Arrays in JavaScriptOftentimes, we need to combine multiple arrays into one array. With multiple arrays, combining them… Append Item to a JavaScript ArrayAppending an item to an array in …

Topic: Combining Arrrays and removing dups, JavaScript Array: Exercise-30 with Solution. Write a JavaScript function to merge two arrays and removes all duplicates elements. if(!assoc[item]) { result_array.​unshift(item); assoc[item] = true; } } return result_array; } var Previous: Write a JavaScript function to fill an array with values (numeric, string with  That way, we filter out the values, and remaining values form an array, and that result array has unique values. The native method filter will loop through the array and leave only those entries that pass the given callback function unique. The unique function checks, if the given value is the first occurring.

JavaScript array: Merge two arrays and removes all duplicates , array_count_values() - Counts all the values of an array In PHP7 there were significant changes to Packed and Immutable arrays resulting in the performance For people looking at the flip flip method for getting unique values in a simple array. Another way to 'unique column' an array, in this case an array of objects: The -Unique switch of Select-Object looks for duplicate items through out the list to remove them. But it does case sensitive search to do that. So, you need to convert the list to lower or upper case before passing it to select-object to get unique items. Sort-Object

array_unique - Manual, Set is a new data object introduced in ES6. Because Set only allows unique values, all duplicates will be removed. And any element that fails or return false , it will be NOT be in the filtered array. The reduce method is used to reduce the elements of the array and combine them into a final array based on some reducer​  phansen nailed it, indeed! For reference, when doing this sort of thing with large collections that you need to get unique values out of, you can often simply combine the arrays and then cast to a HashSet<T> — this is a generic object type that can hold any one specific defined type of objects, and this cast will automatically remove duplicates as a Hashset can only contain unique values.

  • If you're going to use .reduce(), you may as well do the deduping there and then your unique references are Object.keys(references). You can skip the .map() and new Set() portion entirely, saving you 2 iterations through your dataset
  • @mhodges true. I was just thinking of the most straightforward/expressive solution, and the op mentioned they wanted to use Set so I did it that way. I can edit in that answer as well.
  • Well, the OP said they would like to use an "ES6 Pattern", which I don't think is a real requirement. I think that is the OP's attempt at a solution thinking that ES6 is "clearly more efficient than ES5". However, 1 pass through the array with .reduce() is more time and algorithmically efficient than 3 passes through the array. Your for loop solution addresses the issue of the inefficiency, but it's unnecessarily verbose - not to mention, not using any "ES6 Patterns"
  • @mhodges yeah I know. I updated it with what you suggested.
  • You can simplify it even further with Array.from(new Set( => item.reference[0])));
  • For this case, it seems so. But because reference is an array, I'm assuming that there might be multiple values in the future.