Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 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.
- In other words, all values present from all arrays should be included in their original order, but with no duplicates in the final array.
- The unique numbers should be sorted by their original order, but the final array should not be sorted in numerical order.
- */
- function uniteUnique(arr) {
- let temp = [...arguments]; //Create array of passed arguments
- let newArr = [] //New array to hold unique numbers
- for (let obj in temp) {
- for (let item in temp[obj]) //Nested loop to check each (nested) array item
- {
- if (! newArr.includes(temp[obj][item])) { //check if number is not in newArr
- newArr.push(temp[obj][item]) //Push unique items
- }
- }
- }
- return newArr;
- }
- uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]); //returns [1, 3, 2, 5, 4]
Add Comment
Please, Sign In to add comment