How can I get a new array from an argument composed with arrays

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

Challenge: Sorted Union

Link to the challenge:

What is inside of the newArr?

Once you see what the contents of newArr are, this line isn’t quite what you think that it is.

1 Like

Yes, I know what youmean, but I can’t find a way to create a new array composed with the values of arguments, that is question I want to ask.

You have an X-Y problem here.

Focus on the big picture, not a specific fix you think will work.

function uniteUnique(arr) {
  const newArr = Array.from(arguments);
  console.log("newArr:", newArr);
  const finalArr = [];
  for (let i = 0; i < newArr.length; i++) {
    console.log("newArr[i]:", newArr[i]);
    if (finalArr.indexOf(newArr[i]) < 0) {
      finalArr.push(newArr[i])
    }
  }
  return finalArr;
}

uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);

What type of thing is newArr[i]? Is it a number?

1 Like

You can flatten the array.

1 Like

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.

Thanks, I get it, problem solved.

Ahhh, I see what you were thinking. I totally didn’t get that from your description.

‘The value of the arguments’ is, literally, arrays, so you already had an array of all of the arguments, which is where I got lost.

1 Like

That’s OK, Array.from() can get an array, but you can’t combine all of the elements in the same array, the “.flat()” works well.

You already had an array of all the inputs, so you could have also looped over the contents of each sub 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.

… Yes, I’m aware. You get an array of all the arguments, which are arrays. You don’t get an array with the contents of all of the argument arrays.

But if you replace your single loop with a nested loop, then you can use the array of the arguments as is without calling flat…

1 Like

Yes, get what you mean.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.