javascript - Supprimer les valeurs du tableau JS

Mots clés : javascriptarraysduplicatesuniquejavascript

meilleur 5 Réponses javascript - Supprimer les valeurs du tableau JS

vote vote

98

uniq = [...new Set(array)]; 
uniqueArray = a.filter(function(item, pos) {     return a.indexOf(item) == pos; }) 
uniqueArray = a.filter(function(item, pos, self) {     return self.indexOf(item) == pos; }) 
function uniq(a) {     var seen = {};     return a.filter(function(item) {         return seen.hasOwnProperty(item) ? false : (seen[item] = true);     }); } 
function uniq(a) {     var prims = {"boolean":{}, "number":{}, "string":{}}, objs = [];      return a.filter(function(item) {         var type = typeof item;         if(type in prims)             return prims[type].hasOwnProperty(item) ? false : (prims[type][item] = true);         else             return objs.indexOf(item) >= 0 ? false : objs.push(item);     }); } 
function uniq(a) {     return a.sort().filter(function(item, pos, ary) {         return !pos || item != ary[pos - 1];     }); } 
function uniqBy(a, key) {     var seen = {};     return a.filter(function(item) {         var k = key(item);         return seen.hasOwnProperty(k) ? false : (seen[k] = true);     }) } 
a = [[1,2,3], [4,5,6], [1,2,3]] b = uniqBy(a, JSON.stringify) console.log(b) // [[1,2,3], [4,5,6]] 
function uniqBy(a, key) {     var index = [];     return a.filter(function (item) {         var k = key(item);         return index.indexOf(k) >= 0 ? false : index.push(k);     }); } 
function uniqBy(a, key) {     let seen = new Set();     return a.filter(item => {         let k = key(item);         return seen.has(k) ? false : seen.add(k);     }); } 
function uniqBy(a, key) {     return [         ...new Map(             a.map(x => [key(x), x])         ).values()     ] } 
function uniqByKeepFirst(a, key) {     let seen = new Set();     return a.filter(item => {         let k = key(item);         return seen.has(k) ? false : seen.add(k);     }); }   function uniqByKeepLast(a, key) {     return [         ...new Map(             a.map(x => [key(x), x])         ).values()     ] }  //  data = [     {a:1, u:1},     {a:2, u:2},     {a:3, u:3},     {a:4, u:1},     {a:5, u:2},     {a:6, u:3}, ];  console.log(uniqByKeepFirst(data, it => it.u)) console.log(uniqByKeepLast(data, it => it.u))
var result = []; a.forEach(function(item) {      if(result.indexOf(item) < 0) {          result.push(item);      } }); 
  $.uniqArray = function(a) {         return $.grep(a, function(item, pos) {             return $.inArray(item, a) === pos;         });   } 
function uniq_fast(a) {     var seen = {};     var out = [];     var len = a.length;     var j = 0;     for(var i = 0; i < len; i++) {          var item = a[i];          if(seen[item] !== 1) {                seen[item] = 1;                out[j++] = item;          }     }     return out; } 
function uniq(a) {     var seen = {};     return a.filter(function(item) {         return seen.hasOwnProperty(item) ? false : (seen[item] = true);     }); }  function uniq_fast(a) {     var seen = {};     var out = [];     var len = a.length;     var j = 0;     for(var i = 0; i < len; i++) {          var item = a[i];          if(seen[item] !== 1) {                seen[item] = 1;                out[j++] = item;          }     }     return out; }  /////  var r = [0,1,2,3,4,5,6,7,8,9],     a = [],     LEN = 1000,     LOOPS = 1000;  while(LEN--)     a = a.concat(r);  var d = new Date(); for(var i = 0; i < LOOPS; i++)     uniq(a); document.write('<br>uniq, ms/loop: ' + (new Date() - d)/LOOPS)  var d = new Date(); for(var i = 0; i < LOOPS; i++)     uniq_fast(a); document.write('<br>uniq_fast, ms/loop: ' + (new Date() - d)/LOOPS)
function uniq(a) {    return Array.from(new Set(a)); } 
let uniq = a => [...new Set(a)]; 
function* uniqIter(a) {     let seen = new Set();      for (let x of a) {         if (!seen.has(x)) {             seen.add(x);             yield x;         }     } }  // example:  function* randomsBelow(limit) {     while (1)         yield Math.floor(Math.random() * limit); }  // note that randomsBelow is endless  count = 20; limit = 30;  for (let r of uniqIter(randomsBelow(limit))) {     console.log(r);     if (--count === 0)         break }  // exercise for the reader: what happens if we set `limit` less than `count` and why
vote vote

80

var names = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"]; var uniqueNames = []; $.each(names, function(i, el){     if($.inArray(el, uniqueNames) === -1) uniqueNames.push(el); }); 
vote vote

76

var names = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"];  var uniq = names.reduce(function(a,b){     if (a.indexOf(b) < 0 ) a.push(b);     return a;   },[]);  console.log(uniq, names) // [ 'Mike', 'Matt', 'Nancy', 'Adam', 'Jenny', 'Carl' ]  // one liner return names.reduce(function(a,b){if(a.indexOf(b)<0)a.push(b);return a;},[]); 
var uniq = names.slice() // slice makes copy of array before sorting it   .sort(function(a,b){     return a > b;   })   .reduce(function(a,b){     if (a.slice(-1)[0] !== b) a.push(b); // slice(-1)[0] means last item in array without removing it (like .pop())     return a;   },[]); // this empty array becomes the starting value for a  // one liner return names.slice().sort(function(a,b){return a > b}).reduce(function(a,b){if (a.slice(-1)[0] !== b) a.push(b);return a;},[]); 
var uniq = [ ...new Set(names) ]; // [ 'Mike', 'Matt', 'Nancy', 'Adam', 'Jenny', 'Carl' ] 
var names = ['Mike', 'Matt', 'Nancy', 'Adam', 'Jenny', 'Nancy', 'Carl']  var uniq = names   .map((name) => {     return {count: 1, name: name}   })   .reduce((a, b) => {     a[b.name] = (a[b.name] || 0) + b.count     return a   }, {})  var sorted = Object.keys(uniq).sort((a, b) => uniq[a] < uniq[b])  console.log(sorted) 
vote vote

67

function remove_duplicates(arr) {     var obj = {};     var ret_arr = [];     for (var i = 0; i < arr.length; i++) {         obj[arr[i]] = true;     }     for (var key in obj) {         ret_arr.push(key);     }     return ret_arr; } 
function remove_duplicates_safe(arr) {     var seen = {};     var ret_arr = [];     for (var i = 0; i < arr.length; i++) {         if (!(arr[i] in seen)) {             ret_arr.push(arr[i]);             seen[arr[i]] = true;         }     }     return ret_arr;  } 
function remove_duplicates_es6(arr) {     let s = new Set(arr);     let it = s.values();     return Array.from(it); } 
a = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"];  b = remove_duplicates(a); // b: // ["Adam", "Carl", "Jenny", "Matt", "Mike", "Nancy"]  c = remove_duplicates_safe(a); // c: // ["Mike", "Matt", "Nancy", "Adam", "Jenny", "Carl"]  d = remove_duplicates_es6(a); // d: // ["Mike", "Matt", "Nancy", "Adam", "Jenny", "Carl"] 
vote vote

56

arr = arr.filter(function (value, index, array) {      return array.indexOf(value) === index; }); 

Questions similaires