Not sure how efficient mapping within the filter is, but: const arr = [{ id: 1 }, { id: 2 }, { id: 1 }]; const unique = arr.filter((v, i, a) => a.map(({ id }) => id).indexOf(v.id) === i); console.log(unique); That's assuming id is your primary key Answer from Deleted User on reddit.com
Medium
dpericich.medium.com › how-to-quickly-get-a-unique-array-of-objects-in-javascript-f8a80182ee27
How to Quickly Get A Unique Array of Objects in JavaScript | by Daniel Pericich | Medium
November 1, 2024 - Unlike other scripting languages, JavaScript has no unique iterative method for dealing with collections. Instead, you are forced to use one or more forEach, map, filter, or reduce methods. With a flat array of values, this task is not difficult.
Reddit
reddit.com › r/node › how to get unique set of array of objects in javascript
r/node on Reddit: how to get unique set of array of objects in javascript
June 3, 2020 -
i have an array
friendList = [
{id: 1, fName: 'Sam', lName: 'Brian', profilePic: 'sam.jpg'},
{id: 2, fName: 'Dam', lName: 'Nary', profilePic: 'dam.jpg'},
{id: 3, fName: 'Jam', lName: 'kent', profilePic: 'jam.jpg'},
{id: 1, fName: 'Sam', lName: 'Brian', profilePic: 'sam.jpg'},
{id: 3, fName: 'Jam', lName: 'kent', profilePic: 'jam.jpg'},
]how can i get a unique array of this friendList.. such that the result is :
friendList = [
{id: 1, fName: 'Sam', lName: 'Brian', profilePic: 'sam.jpg'},
{id: 2, fName: 'Dam', lName: 'Nary', profilePic: 'dam.jpg'},
{id: 3, fName: 'Jam', lName: 'kent', profilePic: 'jam.jpg'}
]the solutions i found on the internet are like for primitive type arrays. how to do that with array of objects
Top answer 1 of 9
4
Not sure how efficient mapping within the filter is, but: const arr = [{ id: 1 }, { id: 2 }, { id: 1 }]; const unique = arr.filter((v, i, a) => a.map(({ id }) => id).indexOf(v.id) === i); console.log(unique); That's assuming id is your primary key
2 of 9
2
There are some reasons not to do it this way in all cases, but it works here: Array.from(new Set(friendList.map(JSON.stringify))).map(JSON.parse) https://medium.com/@pmzubar/why-json-parse-json-stringify-is-a-bad-practice-to-clone-an-object-in-javascript-b28ac5e36521
How do I check if the array of objects has duplicates in this case? Feb 3, 2022
r/learnjavascript 4y ago
How to find object in array of objects, who's nested array matches all the objects in a separate array? Dec 21, 2022
r/learnjavascript 3y ago
how to get unique set of array of objects in javascript
Not sure how efficient mapping within the filter is, but: const arr = [{ id: 1 }, { id: 2 }, { id: 1 }]; const unique = arr.filter((v, i, a) => a.map(({ id }) => id).indexOf(v.id) === i); console.log(unique); That's assuming id is your primary key More on reddit.com
unique key in Array of Objects?
Seems like you’re using the wrong data structure for this use case More on reddit.com
Creating a new array from a deeply nested array of objects of arrays, etc.?
Can you give an example of what kind of output you want for the above example? It sounds to me like you may want to flatten? Something like: response .flatMap(obj => obj.hobbies) .flatMap(hobby=> hobby.sports) .flatMap(sport => sport.stats); which should give something like: ["hello", "ending", "location", "2009", "2022", "california", "beginning", "pizza", "banana"] You may want to deduplicate with something like: const unique= Array.from(new Set(allStats)); More on reddit.com
Getting a unique list of values from an array of JavaScript objects
Hmm.... I'd use reduce for that: const newEntries = Array.from( entries.reduce((acc, item) => { acc.add(item.state) return acc }, new Set()) ) newEntries.sort() // optionally sort Then again, I'd probably do it in the data layer instead of nodejs code. SELECT DISTINCT state FROM friends ORDER BY state; Boom! More on reddit.com
Top answer 1 of 16
1355
If you are using ES6/ES2015 or later you can do it this way:
const data = [
{ group: 'A', name: 'SD' },
{ group: 'B', name: 'FI' },
{ group: 'A', name: 'MM' },
{ group: 'B', name: 'CO'}
];
const unique = [...new Set(data.map(item => item.group))]; // [ 'A', 'B']
Here is an example on how to do it.
2 of 16
619
For those who want to return object with all properties unique by key
const array =
[
{ "name": "Joe", "age": 17 },
{ "name": "Bob", "age": 17 },
{ "name": "Carl", "age": 35 }
]
const key = 'age';
const arrayUniqueByKey = [...new Map(array.map(item =>
[item[key], item])).values()];
console.log(arrayUniqueByKey);
/*OUTPUT
[
{ "name": "Bob", "age": 17 },
{ "name": "Carl", "age": 35 }
]
*/
// Note: this will pick the last duplicated item in the list.
Run code snippetEdit code snippet Hide Results Copy to answer Expand
CoreUI
coreui.io › blog › how-to-get-unique-values-from-a-javascript-array
How to Get Unique Values from a JavaScript Array · CoreUI
July 5, 2024 - For example, to get unique objects by the age property: const array = [ { name: 'Alice', age: 25 }, { name: 'Bob', age: 25 }, { name: 'Charlie', age: 30 } ] const uniqueArray = array.filter((value, index, self) => index === self.findIndex((t) => ( t.age === value.age )) ) console.log(uniqueArray) // Output: [{ name: 'Alice', age: 25 }, { name: 'Charlie', age: 30 }] This method uses findIndex to ensure only the first occurrence of each age is included.
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-return-an-array-of-unique-objects-in-javascript
How to Return an Array of Unique Objects in JavaScript ? - GeeksforGeeks
July 23, 2025 - Example: The below code uses a set to return an array of unique objects. ... // input arr let arr = [ { articleId: 101, title: "Introduction to JavaScript" }, { articleId: 102, title: "Arrays in JavaScript" }, { articleId: 101, title: "Introduction to JavaScript" }, { articleId: 103, title: "Functions in JavaScript" }, ]; // Set to get unique objects let setObj = new Set(arr.map(JSON.stringify)); let output = Array.from(setObj).map(JSON.parse); // Output console.log(output);
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-convert-array-of-objects-into-unique-array-of-objects-in-javascript
How to Convert Array of Objects into Unique Array of Objects in JavaScript ? - GeeksforGeeks
July 23, 2025 - This approach uses the some() method to create a unique array of objects. The some() method checks if at least one element in the array passes the test implemented by the provided function.
DEV Community
dev.to › phibya › methods-to-get-unique-values-from-arrays-in-javascript-and-their-performance-1da8
Methods to get unique values from arrays in Javascript and their performance - DEV Community
February 21, 2022 - Without further intro, let's just dive into the solutions: use Array.prototype.reduce AND (Set for primitive values OR Map for objects). Here are the code: For an array of primitive values · const uniqueArray = (array) => { return Array.from( array.reduce((set, e) => set.add(e), new Set()) ) } console.log(uniqueArray([1,2,2,3,3,3])) // [1, 2, 3] //OR simply const uniqueArray = (array) => Array.from(new Set(array))
Janik von Rotz
janikvonrotz.ch › 2022 › 01 › 18 › javascript-get-array-with-unique-objects
Janik von Rotz - JavaScript: Get array with unique objects
January 18, 2022 - In JavaScript you can use Set to get an array with unique items. However, this does not work with objects. Converting the object to a string before creating the set is the solution.
TutorialsPoint
tutorialspoint.com › extract-unique-objects-by-attribute-from-an-array-of-objects-in-javascript
Extract unique objects by attribute from an array of objects in JavaScript
January 17, 2023 - Step 7 ? The uniqueEmployees array contains all objects with unique emp_id, and users can iterate through it to get object values. In the example below, we have implemented the algorithm to extract the unique objects by attribute using the Map and for-of loop.
YouTube
youtube.com › watch
Get a Unique List of Objects in an Array of Object in JavaScript - YouTube
To the tutorial! : https://yagisanatode.com/2021/07/03/get-a-unique-list-of-objects-in-an-array-of-object-in-javascript/00:00 Intro00:38 Build the array of o...
Published July 3, 2021
Medium
xinyustudio.medium.com › javascript-get-unique-array-elements-of-objects-remove-duplicates-in-one-line-code-yes-f54867ae2dd2
JavaScript: Get unique array elements of OBJECTS, remove duplicates in ONE-LINE code, yes! | by David Kou | Medium
October 12, 2019 - The question, in short, is to get the unique element in an array of objects (not primitives like integer), in addition, the object may not be exactly the same! Note the space highlighted in below snapshot! I pondered a while and wish to come out a solution, but before that, I googled it, and found so many solutions: How to Remove Array Duplicates in ES6 — DailyJS — Medium · Remove duplicate values from JS array — Stack Overflow · JavaScript: Remove Duplicates from an Array — William Vincent
JavaScript in Plain English
javascript.plainenglish.io › the-easy-way-to-create-a-unique-array-of-json-objects-in-javascript-5634254b17aa
The easy way to create a unique array of JSON Objects in JavaScript | by Lewis Donovan | JavaScript in Plain English
September 18, 2024 - The easy way to create a unique array of JSON Objects in JavaScript No filters, no callbacks, no dependencies and no 'for' loop 🙌 🎉 Google “JS array of unique JSON Objects” and you’ll be …
ServiceNow Community
servicenow.com › community › developer-forum › how-to-find-unique-set-of-objects-in-an-array › m-p › 1559831
Solved: How to find unique set of objects in an array - ServiceNow Community
May 21, 2022 - var obj = '[ { "name": "Joe", "age": 17,flag:"true" },{ "name": "Bob", "age": 17 ,flag:"true" },{ "name": "Carl", "age": 35,flag:"false" },{ "name": "Joe", "age": 17,flag:"true" }]'; var uni = []; var parser = new JSONParser(); var parameterArr = parser.parse(obj); for (var i = 0; i < parameterArr.length; i++) { uni.push(parameterArr[i].name.replace(/\s/g, '')); } var noDuplicates = new ArrayUtil().unique(uni.toString().split(',')); gs.print(noDuplicates);
LearnersBucket
learnersbucket.com › home › examples › javascript › javascript get unique items from array
Javascript get unique items from array - LearnersBucket
May 3, 2019 - We can use the spread … along with map and Set to get the unique properties from the array of objects.
Better Programming
betterprogramming.pub › how-to-find-unique-objects-in-an-array-in-javascript-by-object-reference-or-key-value-pairs-131338898d7a
How to Find Unique Objects in an Array in JavaScript by Object Reference or Key-Value Pairs | by Dr. Derek Austin 🥳 | Better Programming
January 5, 2023 - I’ll be exploring using Set to remove any duplicate objects from the array. First, I’ll find unique objects based on the objects’ variable name (objects with the same object reference). Then, I’ll filter for objects with identical key-value pairs (that have the same content but different object references). Before I get to coding, let’s discuss the difference between object reference and object content because it illuminates how Set works in JavaScript.