daily pastebin goal
45%
SHARE
TWEET

Untitled

a guest Feb 18th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Write a function that takes two or more arrays and returns a new array of unique values in the order of the original provided arrays.
  2. In other words, all values present from all arrays should be included in their original order, but with no duplicates in the final array.
  3. The unique numbers should be sorted by their original order, but the final array should not be sorted in numerical order.
  4. */
  5.  
  6. function uniteUnique(arr) {
  7.   let temp = [...arguments];  //Create array of passed arguments
  8.   let newArr = []     //New array to hold unique numbers
  9.   for (let obj in temp) {
  10.     for (let item in temp[obj]) //Nested loop to check each (nested) array item
  11.     {
  12.       if (! newArr.includes(temp[obj][item])) { //check if number is not in newArr
  13.         newArr.push(temp[obj][item])     //Push unique items
  14.       }
  15.     }    
  16.   }
  17.   return newArr;
  18. }
  19.  
  20. uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);  //returns [1, 3, 2, 5, 4]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top