“Jak znaleźć unikalne wartości z tablicy w sortowanej kolejności JS” Kod odpowiedzi

Znajdź unikalne elementy w tablicy JavaScript

let a = ["1", "1", "2", "3", "3", "1"];
let unique = a.filter((item, i, ar) => ar.indexOf(item) === i);
console.log(unique);
Funny Finch

Jak znaleźć unikalne wartości w tablicy w JS za pomocą funkcji

// 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)
Puzzled Peafowl

Jak znaleźć unikalne wartości z tablicy w sortowanej kolejności 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));
Imran Kabir

Odpowiedzi podobne do “Jak znaleźć unikalne wartości z tablicy w sortowanej kolejności JS”

Pytania podobne do “Jak znaleźć unikalne wartości z tablicy w sortowanej kolejności JS”

Więcej pokrewnych odpowiedzi na “Jak znaleźć unikalne wartości z tablicy w sortowanej kolejności JS” w JavaScript

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu