Array Remove Duplicate Results Javascript

匆匆过客 提交于 2019-12-24 20:33:23

问题


I need to remove the duplicates after they are shuffled. Currently the results come out with duplicates.

Example: Results 2,2,1,4,4,3,5,5, I need as 2,1,4,3,5

This is a large array

<script>
Array.prototype.shuffle = function() {
var input = this;

for (var i = input.length-1; i >=0; i--) {

    var randomIndex = Math.floor(Math.random()*(i+1)); 
    var itemAtIndex = input[randomIndex]; 

    input[randomIndex] = input[i]; 
    input[i] = itemAtIndex;

}
return input;
}

var tempArray = [ 

1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,

2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,

3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,

4,4,4,4,4,4,4,4,4,4,4,4,4,

5,5,5,5,5,
]

tempArray.shuffle();

document.write(tempArray);

</script>

回答1:


Instead of using that large array, simply use [1,2,3,4,5].shuffle(). This way, you won't get duplicates. But here's a function that will give you a unique array, i.e., an array without duplicates:

function unique(arr) {
  var result = [],
      map = {};
  for (var i = 0; i < arr.length; i++) {
    var duplicate = map[arr[i]];
    if (!duplicate) {
        result.push(arr[i]);
        map[arr[i]] = true;
    }
  }
  return result;
}

Then, just use unique(tempArray.shuffle()).

Here's a DEMO.




回答2:


function unique(b) {
    for (var c = [], d = {}, a = 0; a < b.length; a++) {
        d[b[a]] || (c.push(b[a]), d[b[a]] = !0);
    }
    return c;
}

unique( [1, 1, 1, 2, 2, 3] ); // [1, 2, 3]



回答3:


You can use the following approach, using ES6 syntax: const unique = [...new Set(array)]

More about the Set object: Mozilla - Set object




回答4:


If you have access to jQuery, you could split the process into two arrays, take that result, loop through it and only add it to the newArray if it's not already in there. This way they come out in the same order.

var someArray = [3,3,3,3,3,3,4,4,4,4,1,1,1,1,1,2,2];

function cleanArray(oldArray) {
    var newArray = [];
    for(i=0;i<oldArray.length;i++) {
        if( $.inArray(oldArray[i], newArray) == -1 ) {
            newArray.push(oldArray[i]);         
        }
    }
    return newArray;
}

document.write(cleanArray(someArray));
//result would be 3,4,1,2

EDIT: I've updated the function so it works the way I believe you imagined. Here's a working example: http://jsfiddle.net/xe2F8/

Also, don't forget to link to jquery:

    <script src="http://code.jquery.com/jquery-latest.js"></script>



回答5:


One really quick way of removing duplicates from any JavaScript array is the following:

var listWithDuplicates = [1, 2, 2, 4, 3, 4, 5, 3, 1, 5];
console.log(listWithDuplicates.filter(function(element, index, array) {
  // using lastIndexOf(...) retains the last
  // repetition preserving the order of the array
  return index === array.indexOf(element);
}));
//prints out: [1, 2, 4, 3, 5] - with indexOf variant
//prints out: [2, 4, 3, 1, 5] - with lastIndexOf variant

Hope this comes in handy for some purpose related to removing duplicates from JavaScript arrays.

Please share feedback about any corner cases not addressed by this solution or any suggestions for improving this answer.



来源:https://stackoverflow.com/questions/12166205/array-remove-duplicate-results-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!