Skip to content
Advertisement

Returning array values, not the array iterator in Javascript

How can I return the values of an array? The methods .values() and .entries() seem to return an Array Iterator. This is not what I want. I am also not allowed to modify func1() function for this edge case.

JavaScript

Thanks!

Advertisement

Answer

As Bergi stated, func1() will always return an array, no matter what func2() returns. But there are a couple of ways to achieve this based on the return value of func1().

You can either simply use the first element in the array test[0], you can use the Array.flat() method, or the spread operator. See the snippet below.

JavaScript
Advertisement