admin管理员组

文章数量:1295066

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?

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?

Share Improve this question edited Jul 14, 2016 at 13:38 springogeek 3223 silver badges7 bronze badges asked Jul 14, 2016 at 11:46 Max LynnMax Lynn 1,7786 gold badges25 silver badges35 bronze badges 2
  • how about using reduce for same purpose? here's the fiddle with numbers instead of strings, but I don't think it should matter jsfiddle.net/58z7nrfy – llamerr Commented Jul 14, 2016 at 11:59
  • You're looking after array of strings or array of any? Can you add some use cases or any example of what are you about (something like before and after). – Morteza Tourani Commented Jul 14, 2016 at 13:53
Add a comment  | 

7 Answers 7

Reset to default 8

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']

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, "", ""]

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"]

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);

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+)

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]

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])
      }
    }

本文标签: javascriptThe best way to remove duplicate strings in an arrayStack Overflow