javascript array remove duplicates
JavaScript
// 1. filter()
function removeDuplicates(array) {
return array.filter((a, b) => array.indexOf(a) === b)
};
// 2. forEach()
function removeDuplicates(array) {
let x = {};
array.forEach(function(i) {
if(!x[i]) {
x[i] = true
}
})
return Object.keys(x)
};
// 3. Set
function removeDuplicates(array) {
array.splice(0, array.length, ...(new Set(array)))
};
// 4. map()
function removeDuplicates(array) {
let a = []
array.map(x =>
if(!a.includes(x) {
a.push(x)
})
return a
};
/THIS SITE/ "https://dev.to/mshin1995/back-to-basics-removing-duplicates-from-an-array-55he#comments"const names = ['John', 'Paul', 'George', 'Ringo', 'John'];
let x = (names) => names.filter((v,i) => names.indexOf(v) === i)
x(names); // 'John', 'Paul', 'George', 'Ringo'const names = ['John', 'Paul', 'George', 'Ringo', 'John'];
let x = (names) => names.filter((v,i) => names.indexOf(v) === i)
x(names); // 'John', 'Paul', 'George', 'Ringo'
const names = ['John', 'Paul', 'George', 'Ringo', 'John'];
let unique = [...new Set(names)];
console.log(unique); // 'John', 'Paul', 'George', 'Ringo'var data = [1, 2, 'Peter', 3, 3,'Peter', 2, 2, 1, 2, 3, 4, 4, 5, 4];
var unique = data.filter((v, i, a) => a.indexOf(v) === i);function toUniqueArray(a){
var newArr = [];
for (var i = 0; i < a.length; i++) {
if (newArr.indexOf(a[i]) === -1) {
newArr.push(a[i]);
}
}
return newArr;
}
var colors = ["red","red","green","green","green"];
var colorsUnique=toUniqueArray(colors); // ["red","green"]
Also in JavaScript:
- Title
- get array by array of indices js
- Category
- JavaScript
- Title
- email regex javascript
- Category
- JavaScript
- Title
- app script append two list
- Category
- JavaScript
- Title
- get the index of object in array
- Category
- JavaScript
- Title
- add item to list javascript
- Category
- JavaScript
- Title
- how to create duplicate key array in javascript
- Category
- JavaScript
- Title
- javascript .split().reverse.join
- Category
- JavaScript
- Title
- how to access array datat in class component react
- Category
- JavaScript
- Title
- angular add debounce time before putting valu in subject next
- Category
- JavaScript
- Title
- how to make text come up with javascript
- Category
- JavaScript
- Title
- angular attach component to body
- Category
- JavaScript
- Title
- generate thumbnail of pdf using pf js
- Category
- JavaScript
- Title
- html parser javascript
- Category
- JavaScript
- Title
- call javascript function after page load complete
- Category
- JavaScript
- Title
- how to add object to array javascript
- Category
- JavaScript
- Title
- how to write in js
- Category
- JavaScript
- Title
- if (req.url === "/script.js")
- Category
- JavaScript
- Title
- how to add class to element on mouseclick with js
- Category
- JavaScript
- Title
- forever loop in js
- Category
- JavaScript
- Title
- chai js
- Category
- JavaScript
- Title
- discord.js how to go back a file
- Category
- JavaScript
- Title
- angular material multi select ngmodel
- Category
- JavaScript
- Title
- how to update all node libraries
- Category
- JavaScript
- Title
- how to add changes every time you route navigate to page in angular
- Category
- JavaScript
- Title
- insert into mongodb node js
- Category
- JavaScript
- Title
- http request javascript
- Category
- JavaScript
- Title
- injected stylesheet remove
- Category
- JavaScript
- Title
- add dev dependency yarn
- Category
- JavaScript
- Title
- how to verify timestamp format in javascript
- Category
- JavaScript
- Title
- change node version
- Category
- JavaScript
- Title
- check if variable is undefined or null jquery
- Category
- JavaScript
- Title
- interceptor in angular 8
- Category
- JavaScript
- Title
- currying in javascript
- Category
- JavaScript
- Title
- drupal 8 node has field
- Category
- JavaScript
- Title
- inner html jquery
- Category
- JavaScript
- Title
- ${ js
- Category
- JavaScript
- Title
- how to debug javascript
- Category
- JavaScript
- Title
- flowjs attributes
- Category
- JavaScript
- Title
- angular reactive forms
- Category
- JavaScript
- Title
- html external javascript
- Category
- JavaScript
- Title
- get today's date javascript
- Category
- JavaScript
- Title
- is checked checkbox jquery
- Category
- JavaScript
- Title
- iterate through array javascript
- Category
- JavaScript
- Title
- fill array with values javascript
- Category
- JavaScript
- Title
- how to check if a string has only alphabets in javascript
- Category
- JavaScript
- Title
- how to make a inventory in js
- Category
- JavaScript
- Title
- forward and reverse loop one by one js
- Category
- JavaScript
- Title
- array find method javascript
- Category
- JavaScript
- Title
- counter exapmle in vue js
- Category
- JavaScript
- Title
- fix your timestep javascript
- Category
- JavaScript
- Title
- how to get today date in javascript
- Category
- JavaScript
- Title
- A cookie associated with a cross-site resource at http://placeholder.com/ was set without the `SameSite` attribute.
- Category
- JavaScript
- Title
- if statement in javascript
- Category
- JavaScript
- Title
- enzyme check state
- Category
- JavaScript
- Title
- ajax with progress bar
- Category
- JavaScript
- Title
- iframe loader jquery
- Category
- JavaScript
- Title
- bubble sort
- Category
- JavaScript
- Title
- how to do bubble sort in javascript
- Category
- JavaScript
- Title
- display none js
- Category
- JavaScript
- Title
- how to open new tab in vscode using keyboard
- Category
- JavaScript
- Title
- how to get last item in array javascript
- Category
- JavaScript
- Title
- check if objects are equal javascript
- Category
- JavaScript
- Title
- dropdown list value react fragment
- Category
- JavaScript
- Title
- class javascript
- Category
- JavaScript
- Title
- fsharp newtonsoft json deserialize
- Category
- JavaScript
- Title
- 2gis nuxt
- Category
- JavaScript
- Title
- javascript and
- Category
- JavaScript
- Title
- componentWillUnmount functional component for asynchronous requests
- Category
- JavaScript
- Title
- javascript bigint
- Category
- JavaScript
- Title
- javascript .foreach
- Category
- JavaScript
- Title
- get value javascript
- Category
- JavaScript
- Title
- hasownproperty.call
- Category
- JavaScript
- Title
- how to print console in javascript
- Category
- JavaScript
- Title
- greater than or equal to javascript
- Category
- JavaScript
- Title
- check if var is array js
- Category
- JavaScript
- Title
- html get class property
- Category
- JavaScript
- Title
- how to animate a flash in jquery
- Category
- JavaScript
- Title
- how to add javascript to html
- Category
- JavaScript
- Title
- debug node
- Category
- JavaScript
- Title
- how to rescale a canvas in html5 without blurrriness
- Category
- JavaScript
- Title
- inline javascript html
- Category
- JavaScript
- Title
- forEach
- Category
- JavaScript
- Title
- enter event in jquery
- Category
- JavaScript
- Title
- find substring in string javascript
- Category
- JavaScript
- Title
- box shadow javascript style change
- Category
- JavaScript
- Title
- how to convert time to am pm in javascript
- Category
- JavaScript
- Title
- javascript change attribute
- Category
- JavaScript
- Title
- compare dates in javascript
- Category
- JavaScript
- Title
- how to add element in json object
- Category
- JavaScript
- Title
- how to include local image files in javascript object
- Category
- JavaScript
- Title
- component navigation without changin the url react router
- Category
- JavaScript
- Title
- check row empty array javascript
- Category
- JavaScript
- Title
- form taglib in jsp
- Category
- JavaScript
- Title
- how to add role in discord.js v12
- Category
- JavaScript
- Title
- add element to array javascript
- Category
- JavaScript
- Title
- Heroku H10-App Crashed Error
- Category
- JavaScript
- Title
- add class jquery
- Category
- JavaScript
- Title
- how to update kali linux on virtualbox
- Category
- JavaScript
- Title
- angular one way property binding
- Category
- JavaScript
- Title
- deprecationwarning: mongoose
- Category
- JavaScript