I need to create a new array with the value of arguments, then I can use for loop to check the duplicated elements, but there is something wrong with my code, the second line I think, anybody can help me with this? Thanks!
Quesion: Sorted Union
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.
Check the assertion tests for examples. Your code so far
function uniteUnique(arr) {
const newArr = Array.from(arguments);
const finalArr = [];
for (let i = 0; i < newArr.length; i++) {
if (finalArr.indexOf(newArr[i]) < 0) {
finalArr.push(newArr[i])
}
}
return finalArr;
}
uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36
Sorry, I can’t agree with you on this problem, I’ve solved it with the .flat() to get the array I want, then use the for loop to get the unique element array.
Array.from() turns out: [[1, 3, 2], [5, 2, 1, 4], [2, 1]], while Array.from().flat() turns out: [1, 3, 2, 5, 2, 1, 4, 2, 1]. I failed to loop the first one while successed at the second one.