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:

Convert and download html to pdf without saving file

28-10-2018 node js convert html to pdf pdf download pdf without saving

How to submit form using javascript or jquery.

16-04-2017 javascript jquery submit form

How to redirect one page to another page in php

20-03-2017 redirect php

Sanitization in java sript

14-03-2017 sanitization java script

How to find Angular Errors

06-03-2017 errors angularjs exceptionHandler