Sorted Union oh dear help me

Ok i’m trying to iterate over a two dimensional array and push all the values to a new one dimensional array in order. I thought this was a simple thing but I’ve lost like three days over this.

I’ve tried two for loops, i’ve tried .concat(), I’ve tried a reduce method, I’ve tried using spread operators. Literally nothing works. My function just returns an empty array every time. I’m on the edge of insanity here.

I’ve tried this in Visual Studio Code and Google Dev Tools. Both return empty arrays.

function uniteUnique(arr) {
  var myNewArray = [];
for (var i = 0; i < arr.length; i++) {
  for (var j = 0; j < arr[i].length; j++) {
    myNewArray.push(arr[i][j]);
  }
}
return myNewArray;

  }

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

Where does myArray come from?

Sorry. I was so frustrated it was a copy and pasted version of exactly what I had written from a tutorial website on javascript. I changed it back to the original arr, and it still doesn’t work. Still just returns and empty array.

No problem.

I can see where the issue is.
Your function argument (the thing you pass to a function) is not what you expect inside it.

Try to console.log() things in different places, before the loop, inside it, after…
Things like console.log(arr[i]) here and there, console.log(arr[i][j]), console.log(arr).

If you will still need help I will give you a solution.

Oh wow. I just thought it would be arrays one after each other. I didn’t expect it to be a key and value type of thing. I put in return arguments and got this.

{ '0': [ 1, 3, 2 ], '1': [ 5, 2, 1, 4 ], '2': [ 2, 1 ] }

Is this just what happens every time you put a multidimensional array into a function, or did I somehow copy and paste it from freecodecamp without it being obvious?

What’s really confusing me now is what is literally passed into the function from what I can see isn’t a key and value type object. It’s just a two dimensional array.

Your example is working, but that is not what’s happening.
What you need is nested arrays.
function([[],[],[]])
What you did before was :
function([],[],[])

Your last example:
function({0:[], 1:[], 2:[]})
is working too, but that is because you manually added key names. With nested arrays you don’t need to do that.

Thanks so much for helping me with this.

I think I understand a little of what you’re saying. This isn’t one argument of a multidimensional array, it’s multiple arrays represented as multiple arguments. So I can only iterate over a nested array with a for loop the way I’ve written it.

So how would I extract the info in this case? Should I use arguments in the function and then just extract the values and not the keys?

I may not understand your last post correctly.

Your function should be working properly if you pass arguments as a single object.

funct({0:[],1:[]....n:[]}) should work. You pass a single object containing key:value pairs. It will work only if keys are numbers. It can have some errors. I would not pass arguments like these.

funct([[],[],...[]]) should also work. You pass a single array containing other arrays. I would use this.

If you want your function to take 3 or more arguments, like:
function([a],[b],[c],....[n]) you can read about it on MDN.

I would also prefer to use the third option but that isn’t how the challenge is formatted in the curriculum. I think that’s why I’m having this problem.

In the curriculum the function is passed multiple arguments that are themselves a singular array. I didn’t manually add the keys, when I put return arguments in the function that’s what came out of it.

I’m trying to iterate over multiple arguments that are themselves singular arrays. I want to extract what’s in them and construct one big array.

This challenge, correct?


You can also use the rest parameter to have the function accept an unspecified number of arguments. Or use the arguments object as already suggested.

Post your current code if it’s different from the code in the first post.

Hi there! Still need help? I just made a ‘flexible’ solution to your coding problem. Let me know. Thanks.