Filter out array to have only unique values











up vote
11
down vote

favorite
3












I need to filter out my array to contain only unique values.
this is my array data



["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"]


expected result should be



["X_row7", "X_row4", "X_row6", "X_row10", "X_row11", "X_row8"]


How should i continue my code to get proper result.



newArray = ;
for(n in data){
if(!newArray.indexOf(n)){
newArray.push(n);
}
}
console.log(newArray);


If you need any additional information's please let me know and i will provide. thank you










share|improve this question






















  • Possible duplicate of Unique values in an array
    – Schleis
    Jul 5 '16 at 15:10










  • ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"].filter(function(v,i,arr){ return i==arr.indexOf(v); })
    – Pranav C Balan
    Jul 5 '16 at 15:11










  • Possible duplicate of How to get unique values in an array
    – Adeel Imran
    Dec 25 '17 at 19:34















up vote
11
down vote

favorite
3












I need to filter out my array to contain only unique values.
this is my array data



["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"]


expected result should be



["X_row7", "X_row4", "X_row6", "X_row10", "X_row11", "X_row8"]


How should i continue my code to get proper result.



newArray = ;
for(n in data){
if(!newArray.indexOf(n)){
newArray.push(n);
}
}
console.log(newArray);


If you need any additional information's please let me know and i will provide. thank you










share|improve this question






















  • Possible duplicate of Unique values in an array
    – Schleis
    Jul 5 '16 at 15:10










  • ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"].filter(function(v,i,arr){ return i==arr.indexOf(v); })
    – Pranav C Balan
    Jul 5 '16 at 15:11










  • Possible duplicate of How to get unique values in an array
    – Adeel Imran
    Dec 25 '17 at 19:34













up vote
11
down vote

favorite
3









up vote
11
down vote

favorite
3






3





I need to filter out my array to contain only unique values.
this is my array data



["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"]


expected result should be



["X_row7", "X_row4", "X_row6", "X_row10", "X_row11", "X_row8"]


How should i continue my code to get proper result.



newArray = ;
for(n in data){
if(!newArray.indexOf(n)){
newArray.push(n);
}
}
console.log(newArray);


If you need any additional information's please let me know and i will provide. thank you










share|improve this question













I need to filter out my array to contain only unique values.
this is my array data



["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"]


expected result should be



["X_row7", "X_row4", "X_row6", "X_row10", "X_row11", "X_row8"]


How should i continue my code to get proper result.



newArray = ;
for(n in data){
if(!newArray.indexOf(n)){
newArray.push(n);
}
}
console.log(newArray);


If you need any additional information's please let me know and i will provide. thank you







javascript arrays






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jul 5 '16 at 15:08









Valor_

97822157




97822157












  • Possible duplicate of Unique values in an array
    – Schleis
    Jul 5 '16 at 15:10










  • ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"].filter(function(v,i,arr){ return i==arr.indexOf(v); })
    – Pranav C Balan
    Jul 5 '16 at 15:11










  • Possible duplicate of How to get unique values in an array
    – Adeel Imran
    Dec 25 '17 at 19:34


















  • Possible duplicate of Unique values in an array
    – Schleis
    Jul 5 '16 at 15:10










  • ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"].filter(function(v,i,arr){ return i==arr.indexOf(v); })
    – Pranav C Balan
    Jul 5 '16 at 15:11










  • Possible duplicate of How to get unique values in an array
    – Adeel Imran
    Dec 25 '17 at 19:34
















Possible duplicate of Unique values in an array
– Schleis
Jul 5 '16 at 15:10




Possible duplicate of Unique values in an array
– Schleis
Jul 5 '16 at 15:10












["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"].filter(function(v,i,arr){ return i==arr.indexOf(v); })
– Pranav C Balan
Jul 5 '16 at 15:11




["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"].filter(function(v,i,arr){ return i==arr.indexOf(v); })
– Pranav C Balan
Jul 5 '16 at 15:11












Possible duplicate of How to get unique values in an array
– Adeel Imran
Dec 25 '17 at 19:34




Possible duplicate of How to get unique values in an array
– Adeel Imran
Dec 25 '17 at 19:34












10 Answers
10






active

oldest

votes

















up vote
35
down vote



accepted










You can use Array.filter function to filter out elements of an array based on the return value of a callback function. The callback function runs for every element of the original array.



The logic for the callback function here is that if the indexOf value for current item is same as the index, it means the element has been encountered first time, so it can be considered unique. If not, it means the element has been encountered already, so should be discarded now.






var arr = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"];

var filteredArray = arr.filter(function(item, pos){
return arr.indexOf(item)== pos;
});

console.log( filteredArray );





UPDATE (16 Nov 2017)



If you can rely on ES6 features, then you can use Set object and Spread operator to create a unique array from a given array, as already specified in @Travis Heeter's answer below:



var uniqueArray = [...new Set(array)]





share|improve this answer



















  • 2




    this is brilliant
    – oMiKeY
    Nov 15 '17 at 20:10


















up vote
13
down vote













As of June 15, 2015 you may use Set() to create a unique array:



var uniqueArray = [...new Set(array)]




For your Example:



var data = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"]
var newArray = [...new Set(data)]
console.log(newArray)

>> ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"]





share|improve this answer






























    up vote
    5
    down vote













    You could use a hash table for look up and filter all not included values.






    var data = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"],
    unique = data.filter(function (a) {
    return !this[a] && (this[a] = true);
    }, Object.create(null));

    console.log(unique);








    share|improve this answer






























      up vote
      3
      down vote













      You can use Map and Spread Operator:






      var rawData = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"];

      var unique = new Map();
      rawData.forEach(d => unique.set(d, d));
      var uniqueItems = [...unique.keys()];

      console.log(uniqueItems);








      share|improve this answer




























        up vote
        3
        down vote
















        arr = ["I", "do", "love", "JavaScript", "and", "I", "also", "do", "love", "Java"];

        uniqueArr = [... new Set(arr)];

        // or

        reallyUniqueArr = arr.filter((item, pos, ar) => ar.indexOf(item) === pos)

        console.log(`${uniqueArr}n${reallyUniqueArr}`)








        share|improve this answer

















        • 1




          Please provide context to the code
          – Clíodhna
          Sep 7 '17 at 9:48


















        up vote
        2
        down vote













        You can use reduce to loop the array and get the not duplicate values. Also uses an aux object to get the count of added values.




        var aux = {};

        var newArray = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"].reduce((tot, curr)=>{
        if(!aux[curr]){
        aux[curr] = 1;
        tot.push(curr);
        }
        return tot;
        }, );

        console.log(newArray);








        share|improve this answer




























          up vote
          2
          down vote













          Filtering an array to contain unique values can be achieved using the JavaScript Set and Array.from method, as shown below:



          Array.from(new Set(arrayOfNonUniqueValues));



          Set




          The Set object lets you store unique values of any type, whether
          primitive values or object references.



          Return value A new Set object.




          Array.from()




          The Array.from() method creates a new Array instance from an
          array-like or iterable object.



          Return value A new Array instance.




          Example Code:






          const array = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"]

          const uniqueArray = Array.from(new Set(array));

          console.log("uniqueArray: ", uniqueArray);








          share|improve this answer




























            up vote
            1
            down vote













            A slight variation on the indexOf method, if you need to filter multiple arrays:



            function unique(item, index, array) {
            return array.indexOf(item) == index;
            }


            Use as such:



            arr.filter(unique);





            share|improve this answer




























              up vote
              1
              down vote













              I've always used:



              unique = (arr) => arr.filter((item, i, s) => s.lastIndexOf(item) == i);


              But recently I had to get unique values for:



              ["1", 1, "2", 2, "3", 3]


              And my old standby didn't cut it, so I came up with this:



              uunique = (arr) => Object.keys(Object.assign({}, ...arr.map(a=>({[a]:true}))));





              share|improve this answer




























                up vote
                1
                down vote













                Array.prototype.unique = function () {
                return [...new Set(this)]
                }


                then we can write:



                const arr = [1, 5, 2, 2, 2, 3, 4, 3, 2, 1, 5]
                const uniqueArr = arr.unique()





                share|improve this answer





















                  Your Answer






                  StackExchange.ifUsing("editor", function () {
                  StackExchange.using("externalEditor", function () {
                  StackExchange.using("snippets", function () {
                  StackExchange.snippets.init();
                  });
                  });
                  }, "code-snippets");

                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "1"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                   

                  draft saved


                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38206915%2ffilter-out-array-to-have-only-unique-values%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  10 Answers
                  10






                  active

                  oldest

                  votes








                  10 Answers
                  10






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  35
                  down vote



                  accepted










                  You can use Array.filter function to filter out elements of an array based on the return value of a callback function. The callback function runs for every element of the original array.



                  The logic for the callback function here is that if the indexOf value for current item is same as the index, it means the element has been encountered first time, so it can be considered unique. If not, it means the element has been encountered already, so should be discarded now.






                  var arr = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"];

                  var filteredArray = arr.filter(function(item, pos){
                  return arr.indexOf(item)== pos;
                  });

                  console.log( filteredArray );





                  UPDATE (16 Nov 2017)



                  If you can rely on ES6 features, then you can use Set object and Spread operator to create a unique array from a given array, as already specified in @Travis Heeter's answer below:



                  var uniqueArray = [...new Set(array)]





                  share|improve this answer



















                  • 2




                    this is brilliant
                    – oMiKeY
                    Nov 15 '17 at 20:10















                  up vote
                  35
                  down vote



                  accepted










                  You can use Array.filter function to filter out elements of an array based on the return value of a callback function. The callback function runs for every element of the original array.



                  The logic for the callback function here is that if the indexOf value for current item is same as the index, it means the element has been encountered first time, so it can be considered unique. If not, it means the element has been encountered already, so should be discarded now.






                  var arr = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"];

                  var filteredArray = arr.filter(function(item, pos){
                  return arr.indexOf(item)== pos;
                  });

                  console.log( filteredArray );





                  UPDATE (16 Nov 2017)



                  If you can rely on ES6 features, then you can use Set object and Spread operator to create a unique array from a given array, as already specified in @Travis Heeter's answer below:



                  var uniqueArray = [...new Set(array)]





                  share|improve this answer



















                  • 2




                    this is brilliant
                    – oMiKeY
                    Nov 15 '17 at 20:10













                  up vote
                  35
                  down vote



                  accepted







                  up vote
                  35
                  down vote



                  accepted






                  You can use Array.filter function to filter out elements of an array based on the return value of a callback function. The callback function runs for every element of the original array.



                  The logic for the callback function here is that if the indexOf value for current item is same as the index, it means the element has been encountered first time, so it can be considered unique. If not, it means the element has been encountered already, so should be discarded now.






                  var arr = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"];

                  var filteredArray = arr.filter(function(item, pos){
                  return arr.indexOf(item)== pos;
                  });

                  console.log( filteredArray );





                  UPDATE (16 Nov 2017)



                  If you can rely on ES6 features, then you can use Set object and Spread operator to create a unique array from a given array, as already specified in @Travis Heeter's answer below:



                  var uniqueArray = [...new Set(array)]





                  share|improve this answer














                  You can use Array.filter function to filter out elements of an array based on the return value of a callback function. The callback function runs for every element of the original array.



                  The logic for the callback function here is that if the indexOf value for current item is same as the index, it means the element has been encountered first time, so it can be considered unique. If not, it means the element has been encountered already, so should be discarded now.






                  var arr = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"];

                  var filteredArray = arr.filter(function(item, pos){
                  return arr.indexOf(item)== pos;
                  });

                  console.log( filteredArray );





                  UPDATE (16 Nov 2017)



                  If you can rely on ES6 features, then you can use Set object and Spread operator to create a unique array from a given array, as already specified in @Travis Heeter's answer below:



                  var uniqueArray = [...new Set(array)]





                  var arr = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"];

                  var filteredArray = arr.filter(function(item, pos){
                  return arr.indexOf(item)== pos;
                  });

                  console.log( filteredArray );





                  var arr = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"];

                  var filteredArray = arr.filter(function(item, pos){
                  return arr.indexOf(item)== pos;
                  });

                  console.log( filteredArray );






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 21 '17 at 12:16

























                  answered Jul 5 '16 at 15:11









                  Mohit Bhardwaj

                  5,95611439




                  5,95611439








                  • 2




                    this is brilliant
                    – oMiKeY
                    Nov 15 '17 at 20:10














                  • 2




                    this is brilliant
                    – oMiKeY
                    Nov 15 '17 at 20:10








                  2




                  2




                  this is brilliant
                  – oMiKeY
                  Nov 15 '17 at 20:10




                  this is brilliant
                  – oMiKeY
                  Nov 15 '17 at 20:10












                  up vote
                  13
                  down vote













                  As of June 15, 2015 you may use Set() to create a unique array:



                  var uniqueArray = [...new Set(array)]




                  For your Example:



                  var data = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"]
                  var newArray = [...new Set(data)]
                  console.log(newArray)

                  >> ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"]





                  share|improve this answer



























                    up vote
                    13
                    down vote













                    As of June 15, 2015 you may use Set() to create a unique array:



                    var uniqueArray = [...new Set(array)]




                    For your Example:



                    var data = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"]
                    var newArray = [...new Set(data)]
                    console.log(newArray)

                    >> ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"]





                    share|improve this answer

























                      up vote
                      13
                      down vote










                      up vote
                      13
                      down vote









                      As of June 15, 2015 you may use Set() to create a unique array:



                      var uniqueArray = [...new Set(array)]




                      For your Example:



                      var data = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"]
                      var newArray = [...new Set(data)]
                      console.log(newArray)

                      >> ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"]





                      share|improve this answer














                      As of June 15, 2015 you may use Set() to create a unique array:



                      var uniqueArray = [...new Set(array)]




                      For your Example:



                      var data = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"]
                      var newArray = [...new Set(data)]
                      console.log(newArray)

                      >> ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"]






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Apr 27 '17 at 20:00

























                      answered Apr 27 '17 at 18:55









                      Travis Heeter

                      4,40424068




                      4,40424068






















                          up vote
                          5
                          down vote













                          You could use a hash table for look up and filter all not included values.






                          var data = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"],
                          unique = data.filter(function (a) {
                          return !this[a] && (this[a] = true);
                          }, Object.create(null));

                          console.log(unique);








                          share|improve this answer



























                            up vote
                            5
                            down vote













                            You could use a hash table for look up and filter all not included values.






                            var data = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"],
                            unique = data.filter(function (a) {
                            return !this[a] && (this[a] = true);
                            }, Object.create(null));

                            console.log(unique);








                            share|improve this answer

























                              up vote
                              5
                              down vote










                              up vote
                              5
                              down vote









                              You could use a hash table for look up and filter all not included values.






                              var data = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"],
                              unique = data.filter(function (a) {
                              return !this[a] && (this[a] = true);
                              }, Object.create(null));

                              console.log(unique);








                              share|improve this answer














                              You could use a hash table for look up and filter all not included values.






                              var data = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"],
                              unique = data.filter(function (a) {
                              return !this[a] && (this[a] = true);
                              }, Object.create(null));

                              console.log(unique);








                              var data = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"],
                              unique = data.filter(function (a) {
                              return !this[a] && (this[a] = true);
                              }, Object.create(null));

                              console.log(unique);





                              var data = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"],
                              unique = data.filter(function (a) {
                              return !this[a] && (this[a] = true);
                              }, Object.create(null));

                              console.log(unique);






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 11 '17 at 15:45

























                              answered Jul 5 '16 at 15:11









                              Nina Scholz

                              170k1383147




                              170k1383147






















                                  up vote
                                  3
                                  down vote













                                  You can use Map and Spread Operator:






                                  var rawData = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"];

                                  var unique = new Map();
                                  rawData.forEach(d => unique.set(d, d));
                                  var uniqueItems = [...unique.keys()];

                                  console.log(uniqueItems);








                                  share|improve this answer

























                                    up vote
                                    3
                                    down vote













                                    You can use Map and Spread Operator:






                                    var rawData = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"];

                                    var unique = new Map();
                                    rawData.forEach(d => unique.set(d, d));
                                    var uniqueItems = [...unique.keys()];

                                    console.log(uniqueItems);








                                    share|improve this answer























                                      up vote
                                      3
                                      down vote










                                      up vote
                                      3
                                      down vote









                                      You can use Map and Spread Operator:






                                      var rawData = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"];

                                      var unique = new Map();
                                      rawData.forEach(d => unique.set(d, d));
                                      var uniqueItems = [...unique.keys()];

                                      console.log(uniqueItems);








                                      share|improve this answer












                                      You can use Map and Spread Operator:






                                      var rawData = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"];

                                      var unique = new Map();
                                      rawData.forEach(d => unique.set(d, d));
                                      var uniqueItems = [...unique.keys()];

                                      console.log(uniqueItems);








                                      var rawData = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"];

                                      var unique = new Map();
                                      rawData.forEach(d => unique.set(d, d));
                                      var uniqueItems = [...unique.keys()];

                                      console.log(uniqueItems);





                                      var rawData = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"];

                                      var unique = new Map();
                                      rawData.forEach(d => unique.set(d, d));
                                      var uniqueItems = [...unique.keys()];

                                      console.log(uniqueItems);






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Jul 5 '16 at 17:23









                                      Morteza Tourani

                                      2,71942638




                                      2,71942638






















                                          up vote
                                          3
                                          down vote
















                                          arr = ["I", "do", "love", "JavaScript", "and", "I", "also", "do", "love", "Java"];

                                          uniqueArr = [... new Set(arr)];

                                          // or

                                          reallyUniqueArr = arr.filter((item, pos, ar) => ar.indexOf(item) === pos)

                                          console.log(`${uniqueArr}n${reallyUniqueArr}`)








                                          share|improve this answer

















                                          • 1




                                            Please provide context to the code
                                            – Clíodhna
                                            Sep 7 '17 at 9:48















                                          up vote
                                          3
                                          down vote
















                                          arr = ["I", "do", "love", "JavaScript", "and", "I", "also", "do", "love", "Java"];

                                          uniqueArr = [... new Set(arr)];

                                          // or

                                          reallyUniqueArr = arr.filter((item, pos, ar) => ar.indexOf(item) === pos)

                                          console.log(`${uniqueArr}n${reallyUniqueArr}`)








                                          share|improve this answer

















                                          • 1




                                            Please provide context to the code
                                            – Clíodhna
                                            Sep 7 '17 at 9:48













                                          up vote
                                          3
                                          down vote










                                          up vote
                                          3
                                          down vote












                                          arr = ["I", "do", "love", "JavaScript", "and", "I", "also", "do", "love", "Java"];

                                          uniqueArr = [... new Set(arr)];

                                          // or

                                          reallyUniqueArr = arr.filter((item, pos, ar) => ar.indexOf(item) === pos)

                                          console.log(`${uniqueArr}n${reallyUniqueArr}`)








                                          share|improve this answer















                                          arr = ["I", "do", "love", "JavaScript", "and", "I", "also", "do", "love", "Java"];

                                          uniqueArr = [... new Set(arr)];

                                          // or

                                          reallyUniqueArr = arr.filter((item, pos, ar) => ar.indexOf(item) === pos)

                                          console.log(`${uniqueArr}n${reallyUniqueArr}`)








                                          arr = ["I", "do", "love", "JavaScript", "and", "I", "also", "do", "love", "Java"];

                                          uniqueArr = [... new Set(arr)];

                                          // or

                                          reallyUniqueArr = arr.filter((item, pos, ar) => ar.indexOf(item) === pos)

                                          console.log(`${uniqueArr}n${reallyUniqueArr}`)





                                          arr = ["I", "do", "love", "JavaScript", "and", "I", "also", "do", "love", "Java"];

                                          uniqueArr = [... new Set(arr)];

                                          // or

                                          reallyUniqueArr = arr.filter((item, pos, ar) => ar.indexOf(item) === pos)

                                          console.log(`${uniqueArr}n${reallyUniqueArr}`)






                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Sep 7 '17 at 9:42









                                          Jesse Dirisu

                                          312




                                          312








                                          • 1




                                            Please provide context to the code
                                            – Clíodhna
                                            Sep 7 '17 at 9:48














                                          • 1




                                            Please provide context to the code
                                            – Clíodhna
                                            Sep 7 '17 at 9:48








                                          1




                                          1




                                          Please provide context to the code
                                          – Clíodhna
                                          Sep 7 '17 at 9:48




                                          Please provide context to the code
                                          – Clíodhna
                                          Sep 7 '17 at 9:48










                                          up vote
                                          2
                                          down vote













                                          You can use reduce to loop the array and get the not duplicate values. Also uses an aux object to get the count of added values.




                                          var aux = {};

                                          var newArray = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"].reduce((tot, curr)=>{
                                          if(!aux[curr]){
                                          aux[curr] = 1;
                                          tot.push(curr);
                                          }
                                          return tot;
                                          }, );

                                          console.log(newArray);








                                          share|improve this answer

























                                            up vote
                                            2
                                            down vote













                                            You can use reduce to loop the array and get the not duplicate values. Also uses an aux object to get the count of added values.




                                            var aux = {};

                                            var newArray = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"].reduce((tot, curr)=>{
                                            if(!aux[curr]){
                                            aux[curr] = 1;
                                            tot.push(curr);
                                            }
                                            return tot;
                                            }, );

                                            console.log(newArray);








                                            share|improve this answer























                                              up vote
                                              2
                                              down vote










                                              up vote
                                              2
                                              down vote









                                              You can use reduce to loop the array and get the not duplicate values. Also uses an aux object to get the count of added values.




                                              var aux = {};

                                              var newArray = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"].reduce((tot, curr)=>{
                                              if(!aux[curr]){
                                              aux[curr] = 1;
                                              tot.push(curr);
                                              }
                                              return tot;
                                              }, );

                                              console.log(newArray);








                                              share|improve this answer












                                              You can use reduce to loop the array and get the not duplicate values. Also uses an aux object to get the count of added values.




                                              var aux = {};

                                              var newArray = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"].reduce((tot, curr)=>{
                                              if(!aux[curr]){
                                              aux[curr] = 1;
                                              tot.push(curr);
                                              }
                                              return tot;
                                              }, );

                                              console.log(newArray);








                                              var aux = {};

                                              var newArray = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"].reduce((tot, curr)=>{
                                              if(!aux[curr]){
                                              aux[curr] = 1;
                                              tot.push(curr);
                                              }
                                              return tot;
                                              }, );

                                              console.log(newArray);





                                              var aux = {};

                                              var newArray = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"].reduce((tot, curr)=>{
                                              if(!aux[curr]){
                                              aux[curr] = 1;
                                              tot.push(curr);
                                              }
                                              return tot;
                                              }, );

                                              console.log(newArray);






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Jul 5 '16 at 15:16









                                              Jose Hermosilla Rodrigo

                                              2,04411027




                                              2,04411027






















                                                  up vote
                                                  2
                                                  down vote













                                                  Filtering an array to contain unique values can be achieved using the JavaScript Set and Array.from method, as shown below:



                                                  Array.from(new Set(arrayOfNonUniqueValues));



                                                  Set




                                                  The Set object lets you store unique values of any type, whether
                                                  primitive values or object references.



                                                  Return value A new Set object.




                                                  Array.from()




                                                  The Array.from() method creates a new Array instance from an
                                                  array-like or iterable object.



                                                  Return value A new Array instance.




                                                  Example Code:






                                                  const array = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"]

                                                  const uniqueArray = Array.from(new Set(array));

                                                  console.log("uniqueArray: ", uniqueArray);








                                                  share|improve this answer

























                                                    up vote
                                                    2
                                                    down vote













                                                    Filtering an array to contain unique values can be achieved using the JavaScript Set and Array.from method, as shown below:



                                                    Array.from(new Set(arrayOfNonUniqueValues));



                                                    Set




                                                    The Set object lets you store unique values of any type, whether
                                                    primitive values or object references.



                                                    Return value A new Set object.




                                                    Array.from()




                                                    The Array.from() method creates a new Array instance from an
                                                    array-like or iterable object.



                                                    Return value A new Array instance.




                                                    Example Code:






                                                    const array = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"]

                                                    const uniqueArray = Array.from(new Set(array));

                                                    console.log("uniqueArray: ", uniqueArray);








                                                    share|improve this answer























                                                      up vote
                                                      2
                                                      down vote










                                                      up vote
                                                      2
                                                      down vote









                                                      Filtering an array to contain unique values can be achieved using the JavaScript Set and Array.from method, as shown below:



                                                      Array.from(new Set(arrayOfNonUniqueValues));



                                                      Set




                                                      The Set object lets you store unique values of any type, whether
                                                      primitive values or object references.



                                                      Return value A new Set object.




                                                      Array.from()




                                                      The Array.from() method creates a new Array instance from an
                                                      array-like or iterable object.



                                                      Return value A new Array instance.




                                                      Example Code:






                                                      const array = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"]

                                                      const uniqueArray = Array.from(new Set(array));

                                                      console.log("uniqueArray: ", uniqueArray);








                                                      share|improve this answer












                                                      Filtering an array to contain unique values can be achieved using the JavaScript Set and Array.from method, as shown below:



                                                      Array.from(new Set(arrayOfNonUniqueValues));



                                                      Set




                                                      The Set object lets you store unique values of any type, whether
                                                      primitive values or object references.



                                                      Return value A new Set object.




                                                      Array.from()




                                                      The Array.from() method creates a new Array instance from an
                                                      array-like or iterable object.



                                                      Return value A new Array instance.




                                                      Example Code:






                                                      const array = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"]

                                                      const uniqueArray = Array.from(new Set(array));

                                                      console.log("uniqueArray: ", uniqueArray);








                                                      const array = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"]

                                                      const uniqueArray = Array.from(new Set(array));

                                                      console.log("uniqueArray: ", uniqueArray);





                                                      const array = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"]

                                                      const uniqueArray = Array.from(new Set(array));

                                                      console.log("uniqueArray: ", uniqueArray);






                                                      share|improve this answer












                                                      share|improve this answer



                                                      share|improve this answer










                                                      answered Feb 10 at 23:57









                                                      JSON C11

                                                      3,38443537




                                                      3,38443537






















                                                          up vote
                                                          1
                                                          down vote













                                                          A slight variation on the indexOf method, if you need to filter multiple arrays:



                                                          function unique(item, index, array) {
                                                          return array.indexOf(item) == index;
                                                          }


                                                          Use as such:



                                                          arr.filter(unique);





                                                          share|improve this answer

























                                                            up vote
                                                            1
                                                            down vote













                                                            A slight variation on the indexOf method, if you need to filter multiple arrays:



                                                            function unique(item, index, array) {
                                                            return array.indexOf(item) == index;
                                                            }


                                                            Use as such:



                                                            arr.filter(unique);





                                                            share|improve this answer























                                                              up vote
                                                              1
                                                              down vote










                                                              up vote
                                                              1
                                                              down vote









                                                              A slight variation on the indexOf method, if you need to filter multiple arrays:



                                                              function unique(item, index, array) {
                                                              return array.indexOf(item) == index;
                                                              }


                                                              Use as such:



                                                              arr.filter(unique);





                                                              share|improve this answer












                                                              A slight variation on the indexOf method, if you need to filter multiple arrays:



                                                              function unique(item, index, array) {
                                                              return array.indexOf(item) == index;
                                                              }


                                                              Use as such:



                                                              arr.filter(unique);






                                                              share|improve this answer












                                                              share|improve this answer



                                                              share|improve this answer










                                                              answered Nov 15 '17 at 20:32









                                                              oMiKeY

                                                              2,91351839




                                                              2,91351839






















                                                                  up vote
                                                                  1
                                                                  down vote













                                                                  I've always used:



                                                                  unique = (arr) => arr.filter((item, i, s) => s.lastIndexOf(item) == i);


                                                                  But recently I had to get unique values for:



                                                                  ["1", 1, "2", 2, "3", 3]


                                                                  And my old standby didn't cut it, so I came up with this:



                                                                  uunique = (arr) => Object.keys(Object.assign({}, ...arr.map(a=>({[a]:true}))));





                                                                  share|improve this answer

























                                                                    up vote
                                                                    1
                                                                    down vote













                                                                    I've always used:



                                                                    unique = (arr) => arr.filter((item, i, s) => s.lastIndexOf(item) == i);


                                                                    But recently I had to get unique values for:



                                                                    ["1", 1, "2", 2, "3", 3]


                                                                    And my old standby didn't cut it, so I came up with this:



                                                                    uunique = (arr) => Object.keys(Object.assign({}, ...arr.map(a=>({[a]:true}))));





                                                                    share|improve this answer























                                                                      up vote
                                                                      1
                                                                      down vote










                                                                      up vote
                                                                      1
                                                                      down vote









                                                                      I've always used:



                                                                      unique = (arr) => arr.filter((item, i, s) => s.lastIndexOf(item) == i);


                                                                      But recently I had to get unique values for:



                                                                      ["1", 1, "2", 2, "3", 3]


                                                                      And my old standby didn't cut it, so I came up with this:



                                                                      uunique = (arr) => Object.keys(Object.assign({}, ...arr.map(a=>({[a]:true}))));





                                                                      share|improve this answer












                                                                      I've always used:



                                                                      unique = (arr) => arr.filter((item, i, s) => s.lastIndexOf(item) == i);


                                                                      But recently I had to get unique values for:



                                                                      ["1", 1, "2", 2, "3", 3]


                                                                      And my old standby didn't cut it, so I came up with this:



                                                                      uunique = (arr) => Object.keys(Object.assign({}, ...arr.map(a=>({[a]:true}))));






                                                                      share|improve this answer












                                                                      share|improve this answer



                                                                      share|improve this answer










                                                                      answered Jan 24 at 6:44









                                                                      TennisVisuals

                                                                      16916




                                                                      16916






















                                                                          up vote
                                                                          1
                                                                          down vote













                                                                          Array.prototype.unique = function () {
                                                                          return [...new Set(this)]
                                                                          }


                                                                          then we can write:



                                                                          const arr = [1, 5, 2, 2, 2, 3, 4, 3, 2, 1, 5]
                                                                          const uniqueArr = arr.unique()





                                                                          share|improve this answer

























                                                                            up vote
                                                                            1
                                                                            down vote













                                                                            Array.prototype.unique = function () {
                                                                            return [...new Set(this)]
                                                                            }


                                                                            then we can write:



                                                                            const arr = [1, 5, 2, 2, 2, 3, 4, 3, 2, 1, 5]
                                                                            const uniqueArr = arr.unique()





                                                                            share|improve this answer























                                                                              up vote
                                                                              1
                                                                              down vote










                                                                              up vote
                                                                              1
                                                                              down vote









                                                                              Array.prototype.unique = function () {
                                                                              return [...new Set(this)]
                                                                              }


                                                                              then we can write:



                                                                              const arr = [1, 5, 2, 2, 2, 3, 4, 3, 2, 1, 5]
                                                                              const uniqueArr = arr.unique()





                                                                              share|improve this answer












                                                                              Array.prototype.unique = function () {
                                                                              return [...new Set(this)]
                                                                              }


                                                                              then we can write:



                                                                              const arr = [1, 5, 2, 2, 2, 3, 4, 3, 2, 1, 5]
                                                                              const uniqueArr = arr.unique()






                                                                              share|improve this answer












                                                                              share|improve this answer



                                                                              share|improve this answer










                                                                              answered Nov 19 at 7:20









                                                                              Zuhair Taha

                                                                              6921922




                                                                              6921922






























                                                                                   

                                                                                  draft saved


                                                                                  draft discarded



















































                                                                                   


                                                                                  draft saved


                                                                                  draft discarded














                                                                                  StackExchange.ready(
                                                                                  function () {
                                                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38206915%2ffilter-out-array-to-have-only-unique-values%23new-answer', 'question_page');
                                                                                  }
                                                                                  );

                                                                                  Post as a guest















                                                                                  Required, but never shown





















































                                                                                  Required, but never shown














                                                                                  Required, but never shown












                                                                                  Required, but never shown







                                                                                  Required, but never shown

































                                                                                  Required, but never shown














                                                                                  Required, but never shown












                                                                                  Required, but never shown







                                                                                  Required, but never shown







                                                                                  Popular posts from this blog

                                                                                  Costa Masnaga

                                                                                  Fotorealismo

                                                                                  Sidney Franklin