get unique values from multiple array javascript code example

Example 1: how to find unique elements in array in javascript

let a = ["1", "1", "2", "3", "3", "1"];
let unique = a.filter((item, i, ar) => ar.indexOf(item) === i);
console.log(unique);

Example 2: find unique values between multiple array

var array3 = array1.filter(function(obj) { return array2.indexOf(obj) == -1; });

Example 3: get only unique values from array javascript

arr = [2, 2, 2, 1, 3, 3, 3,3, 4, 5];
arr = arr.sort().filter((item,i)=>!(arr[i] == arr[i+1] || arr[i-1]==arr[i]));
// this will remove all item which are repeating more then one
console.log(arr);
// [1, 4, 5]

Example 4: return uncommon from two arrays js

let array = [1,2,3,4,5,6,78,9];

function except(array,excluded){
let newArr,temp,temp1;

      check1=array.filter(function(value) 
                {
                  return excluded.indexOf(value) == -1; 

                });

      check2=excluded.filter(function(value) 
                {
                  return array.indexOf(value) == -1; 

                });

    output=check1.concat(check2);


    return output;

  }


except(array,[1,2])

//so the output would be => [ 3, 4, 5, 6, 78, 9 ]

Tags:

Misc Example