Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to find unique values in an array in js using function

// app.js

Array.prototype.unique = function() {
  let arr = [];
  for(let i = 0; i < this.length; i++) {
      if(!arr.includes(this[i])) {
          arr.push(this[i]);
      }
  }
  return arr; 
}

const ages = [26, 27, 26, 26, 28, 28, 29, 29, 30]
const uniqueAges = ages.unique()
console.log(uniqueAges)
Comment

How to find unique values from an array in sorted order js

const arr = [1, 1, 1, 3, 2, 2, 8, 3, 4];
const uniqSort = (arr = []) => {
   const map = {};
   const res = [];
   for (let i = 0; i < arr.length; i++) {
      if (!map[arr[i]]) {
         map[arr[i]] = true;
         res.push(arr[i]);
      };
   };
   return res.sort((a, b) => a − b);
};
console.log(uniqSort(arr));
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery option second 
Javascript :: axios in functional component 
Javascript :: js some array 
Javascript :: how to store object in session storage 
Javascript :: javascript var,let,const compare 
Javascript :: javascript convert array to matrix 
Javascript :: javascript how to do else if 
Javascript :: react native keyboard push view up 
Javascript :: object check null or empty 
Javascript :: react native use route params 
Javascript :: disabling ctrl + s using javascript 
Javascript :: json value types 
Javascript :: woocommerce update mini cart ajax 
Javascript :: get file extension file upload control in javascript 
Javascript :: javascript loop replace object values using function 
Javascript :: js var vs let 
Javascript :: jquery fadein to show modal 
Javascript :: javascript find index 
Javascript :: keyup event 
Javascript :: onclick on fragment react 
Javascript :: add and get tokens to securestore expo 
Javascript :: jquery scroll to position 
Javascript :: datatable table header not responsive 
Javascript :: react render twice v18 
Javascript :: How to pass setInterval() into a Javascript class method 
Javascript :: convert html to pdf using javascript 
Javascript :: change react native app name 
Javascript :: lodash find all in array 
Javascript :: jquery window new tab with post 
Javascript :: catch errors async await javascript 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =