Get all unique values in a JavaScript array (remove duplicates)
With JavaScript 1.6 / ECMAScript 5 you can use the native filter
method of an Array in the following way to get an array with unique values:
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
// usage example:
var a = ['a', 1, 'a', 2, '1'];
var unique = a.filter(onlyUnique);
console.log(unique); // ['a', 1, 2, '1']
How to get unique values in an array
Since I went on about it in the comments for @Rocket's answer, I may as well provide an example that uses no libraries. This requires two new prototype functions, contains
and unique
Array.prototype.contains = function(v) { for (var i = 0; i < this.length; i++) { if (this[i] === v) return true; } return false;};
Array.prototype.unique = function() { var arr = []; for (var i = 0; i < this.length; i++) { if (!arr.contains(this[i])) { arr.push(this[i]); } } return arr;}
var duplicates = [1, 3, 4, 2, 1, 2, 3, 8];var uniques = duplicates.unique(); // result = [1,3,4,2,8]
console.log(uniques);
Get unique values from array of arrays
You can use .flat()
to flatten your array and then use Set
to get its unique values.
Demo:
let arr = [ [ "s1@example.com", "s2@example.com" ], [ "s1@example.com", "s3@example.com" ]]
let arr2 = [...new Set(arr.flat(1))];console.log(arr2)
How to get distinct values from an array of objects in JavaScript?
If this were PHP I'd build an array with the keys and take array_keys
at the end, but JS has no such luxury. Instead, try this:
var flags = [], output = [], l = array.length, i;
for( i=0; i<l; i++) {
if( flags[array[i].age]) continue;
flags[array[i].age] = true;
output.push(array[i].age);
}
Filter array to have unique values
You can use Array.filter function to filter out elements of an array based on the return value of a callback function. The callback function runs for every element of the original array.
The logic for the callback function here is that if the indexOf
value for current item is same as the index, it means the element has been encountered first time, so it can be considered unique. If not, it means the element has been encountered already, so should be discarded now.
var arr = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"];
var filteredArray = arr.filter(function(item, pos){ return arr.indexOf(item)== pos; });
console.log( filteredArray );
Getting unique values from an Array for use in ListItem
Edit I misunderstood the question, I would suggest using reduce
.
note I made all require
statements a string for this example to run
const Users = [{
id: 1,
name: "user1",
image: 'require("../assets/1.jpeg")',
location: "Portland"
},
{
id: 2,
name: "user2",
image: 'require("../assets/2.jpeg")',
location: "Portland"
},
{
id: 3,
name: "user3",
image: 'require("../assets/3.jpeg")',
location: "New York"
},
{
id: 4,
name: "user4",
image: 'require("../assets/4.jpeg")',
location: "Pullman"
},
{
id: 5,
name: "user5",
image: 'require("../assets/5.jpeg")',
location: "Portland"
},
{
id: 6,
name: "user6",
image: 'require("../assets/6.jpeg")',
location: "San Francisco"
},
{
id: 7,
name: "user7",
image: 'require("../assets/7.jpeg")',
location: "Pullman"
},
];
console.log(Users.reduce((acc, val) => {
acc[val.location] = acc[val.location] ? [...acc[val.location], val] : [val];
return acc
}, {}))
vba: get unique values from array
This post contains 2 examples. I like the 2nd one:
Sub unique()
Dim arr As New Collection, a
Dim aFirstArray() As Variant
Dim i As Long
aFirstArray() = Array("Banana", "Apple", "Orange", "Tomato", "Apple", _
"Lemon", "Lime", "Lime", "Apple")
On Error Resume Next
For Each a In aFirstArray
arr.Add a, a
Next
On Error Goto 0 ' added to original example by PEH
For i = 1 To arr.Count
Cells(i, 1) = arr(i)
Next
End Sub
Related Topics
How to Fix "Unsupported Class File Major Version 60" in Intellij Idea
Javabean Wrapping with Javafx Properties
Run Command Prompt as Administrator
What Are Good Installanywhere Replacements for Installing a Java Ee Application
How Much Data Can a List Can Hold at the Maximum
Java - Convert Image to Base64
What Are the Pros and Cons of Performing Calculations in SQL VS. in Your Application
JPA Onetomany Not Deleting Child
Implement Converters for Entities with Java Generics
Byte Array and Int Conversion in Java
Java Date Year Calculation Is Off by Year for Two Days
Java 'File.Delete()' Is Not Deleting Specified File
How to Create a Java 8 Localdate from a Long Epoch Time in Milliseconds
Count Words in a String Method
Deprecated Java Httpclient - How Hard Can It Be
Spring MVC + JSON = 406 Not Acceptable
Is There a Difference in Removing the Curly Braces from If Statements in Java