get only unique values from array javascript
// usage example:
var myArray = ['a', 1, 'a', 2, '1'];
var unique = myArray.filter((v, i, a) => a.indexOf(v) === i);
// unique is ['a', 1, 2, '1']const myArray = ['a', 1, 'a', 2, '1'];
const unique = [...new Set(myArray)]; // ['a', 1, 2, '1']const categories = ['General', 'Exotic', 'Extreme', 'Extreme', 'General' ,'Water', 'Extreme']
.filter((value, index, categoryArray) => categoryArray.indexOf(value) === index);
This will return an array that has the unique category names
['General', 'Exotic', 'Extreme', 'Water']
arr = [2, 2, 2, 1, 3, 3, 3,3, 4, 5];
arr = arr.sort().filter((item,i)=>!(arr[i] == arr[i+1] || arr[i-1]==arr[i]));
// this will remove all item which are repeating more then one
console.log(arr);
// [1, 4, 5]
Also in JavaScript:
- how to convert a number to a string in javascript
- random image and link js
- react native expo search bar
- js string to boolean
- click outside box jquery
- js var part of var name
- how to get the id of button clicked in jquery
- js analyse img color
- js window dimensions
- how to create a point in js
- table like another component in react native
- Redirect to any page after 5 seconds in Javascript
- jquery script tag source google
- integers to space separated string in javascript
- how to get only month and year in js
- javascript nested array destructuring
- javascript pareseint
- function redirect javascript
- js log to console
- how to convert jsonobject to json string in java
- json stringify
- redis nodejs
- ReferenceError: Buffer is not defined
- How to get unix timestamp from tomorrow nodejs