Get Unique values from array/object in javascript

19-02-2017 javascript unique remove repeated values from array

Here is the sample array

var Obj = [1,3,4,2,1,2,3,8];
var Obj = Obj.unique();

Result is

[1,3,4,2,8]

Add thease below lines in your script tag. The below code is remove duplicates from array/object

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

 

You might also like:

Use java script file client side and server side at a time in node js
30-07-2017 node js include call javascript
Time Sorting Ascending or Descending Order in Javascript
19-07-2017 Javascript ascending order descending order
How to get id by using name attribute in javascript
20-03-2017 html id name javascript
How to convert html to pdf in Node js
12-03-2017 pdf convert html node js
Get random strings from given string In javascript
21-02-2017 javascript random string