Jquery Function to Get All Unique Elements from an Array

jQuery function to get all unique elements from an array?

You can use array.filter to return the first item of each distinct value-

var a = [ 1, 5, 1, 6, 4, 5, 2, 5, 4, 3, 1, 2, 6, 6, 3, 3, 2, 4 ];
var unique = a.filter(function(itm, i, a) { return i == a.indexOf(itm);});
console.log(unique);

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 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']

jQuery - Finding Distinct Values in Object Array

You could do:

var array = [{
familyName: "one"},
{
familyName: "two"},
{
familyName: "one"},
{
familyName: "two"}];

var dupes = {};
var singles = [];

$.each(array, function(i, el) {

if (!dupes[el.familyName]) {
dupes[el.familyName] = true;
singles.push(el);
}
});

Singles is an array with only DISTINCT objects

EDIT - i have blogged about this and given a more elaborate answer http://newcodeandroll.blogspot.it/2012/01/how-to-find-duplicates-in-array-in.html

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);
}

Jquery get unique values from elements by data type (part 2)

No, equals on arrays does not work that way, you're trying to compare objects, which in this case are different array objects.

You should be inspecting the contents. See this answer for a sensible implementation.

Also, in this case, your use of

var $this = $(this);

is utterly redundant as it's never referenced.

Make a Array with Unique elements in JQuery

jQuery.unique(["cat", "Kitchen", "dog", "Kitchen", "dog", "Kitchen"].join(",").split(","))

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']

Filter array of objects to get distinct values into new array

array.map should work just fine:

var b = a.map(function(e) {
return {
task_project_id: e.task_project_id,
project_description: e.project_description
}
});

Edit: filter out duplicates, by storing a list of already encountered "seen" task project ids:

var seen = {}; // keys = list of unique task project ids
var b = a.filter(function (e) {
return seen[e.task_project_id] ? false : (seen[e.task_project_id] = true);
});

Putting it all together:

var newArray = (function (a) {
var seen = {};
return a.filter(function (e) {
return seen[e.task_project_id] ? false : (seen[e.task_project_id] = true);
}).map(function (e) {
return {
task_project_id: e.task_project_id,
project_description: e.project_description
}
});
})(oldArray);

JavaScript: Get unique values and their counts from an array of objects?

you could use map for this purpose as:

var distances = {};
$.map(electrons,function(e,i) {
distances[e.distance] = (distances[e.distance] || 0) + 1;
});

or

var distances = {};
$.each(electrons,function(i,e) {
distances[this.distance] = (distances[this.distance] || 0) + 1;
});

Also may I point out to you that although this code good to look and compact, this is not generally faster. Better make your code more faster and more easy to look at as:

var distances = {},e;
for (var i = 0,l=electrons.length; i < l; i++) {
e = electrons[i];
distances[e.distance] = (distances[e.distance] || 0) + 1;
}


Related Topics



Leave a reply



Submit