问题
We have managed to create the script below to remove any duplicate strings from an array. However, it is important that we keep the order of the array for when angular loops through them on an ng-repeat. In addition, we want the remaining elements to keep the same index.
scope.feedback = _.map(_.pluck(item.possibleAnswers, 'feedback'), function (element, index, collection) {
return collection.slice(0, index).indexOf(element) === -1 ? element : '';
});
This code above works however we feel like there must be a more simple solution to our problem than this. Has anyone else had a similar problem and come up with a better solution?
回答1:
Variant with reduce https://jsfiddle.net/58z7nrfy/1/
var a = [1,2,3,1,2,3,2,2,3,4,5,5,12,1,23,4,1];
var b = a.reduce(function(p,c,i,a){
if (p.indexOf(c) == -1) p.push(c);
else p.push('')
return p;
}, [])
console.log(b)
[1, 2, 3, "", "", "", "", "", "", 4, 5, "", 12, "", 23, "", ""]
回答2:
Apart from the answers mentioned, you can also use lodash union function like this:
let duplicates = ['Hello', 'Hi', 'Hello'];
let uniques = _.union(duplicates);
Uniques will be: ["Hello", "Hi"]
回答3:
If the target browsers support spread operator then try in your console:
[...new Set(['3','1','1','5'])]
// ['3','1','5']
Or if browser support Array.from you could also write:
Array.from(new Set(['3','1','1','5']))
// ['3','1','5']
回答4:
You could use a Map, which is type prove and prevents iterating again and again with Array#indexOf.
var a = [1, 2, 3, 1, 2, 3, 2, 2, 3, 4, 5, 5, 12, 1, 23, 4, 1, '23'],
filtered = a.filter(function (a) {
if (!this.has(a)) {
this.set(a, true);
return true;
}
}, new Map);
console.log(filtered);
回答5:
Accepted answer is very inefficient. One could use reduce and a hash table or map object to boost the performance. Here I would prefer map in the place of reduce though. Yet I guess extending @Nina Scholz's approach by doubling the maps, the proper answer to OP's question is as follows;
var a = [1, 2, 3, 1, 2, 3, 2, 2, 3, 4, 5, 5, 12, 1, 23, 4, 1, '23'],
unified = a.map(function(e) {
return this.has(e) ? void 0 : (this.set(e,"Cheap Thrills"),e);
}, new Map());
console.log(unified);
If this would be a production code with an arbitrary length array then rather than going with functors, i would implement the map method with standard functions though since it introduces an additional serious performance boost in large arrays. (like in size 10K+)
回答6:
i think lodash uniq()
is very helpful
let data = [1,2,3,1,2,3,2,2,3,4,5,5,12,1,23,4,1];
let uniqData = _.uniq(data ,(e) => {
return e;
});
then output will be:
[1, 2, 3, 4, 5, 12, 23]
回答7:
I used this script:
var words = ['one', 'one', 'two', 'three', 'three', 'two'];
var result = [];
for(i=0;i<words.length;i++){
if(result.indexOf(words[i]) == -1){
result.push(words[i])
}
}
来源:https://stackoverflow.com/questions/38373364/the-best-way-to-remove-duplicate-strings-in-an-array