Get Unique values from array/object in javascript

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:

How to use request module in node js

16-03-2017 request module node js

Get previous date from given date in javascript

06-03-2017 date javascript previous date

Navigate to another page using java script

06-03-2017 javascript redirection navigation

How to get drop down value when drop down change using javascript

27-02-2017 onchange events dropdown javascript functions

Sample Validations in javascript

22-02-2017 validations javascript