1

when i do console.log(myArray) I obtain this:

console.log result

I want to take a value inside only one of this arrays, but how? When i do console.log(array[0]) I obtain this result:

37.7
28.45
36.38
6
  • please explain more clearly what you want to acheive Commented Aug 30, 2021 at 17:38
  • I'm also wondering if Array.prototype.flat() might help you. Commented Aug 30, 2021 at 17:38
  • 1
    Are you saying your input array is: [[37.7], [28.45], [36.38]]? Commented Aug 30, 2021 at 17:40
  • @vavmxd The screenshot you added only shows the output. Could you please add the code for (or screenshot of) all the related code from setting up or fetching the array, to your console.log? Commented Aug 30, 2021 at 19:36
  • Oops, i did not realize that my code have a forEach. The console.log was running 3 times. My bad... Commented Aug 30, 2021 at 20:46

3 Answers 3

1

You have nested arrays. So your main array has three elements each of which contains one number, and the indexes of those arrays go from 0 to 2.

You access each nested array with its index, and then access the number with the 0 index (because there's only one element in that nested array).

const arr = [[37.7], [28.45], [36.38]];

console.log(arr[0][0]);
console.log(arr[1][0]);
console.log(arr[2][0]);

Or even loop over the array and destructure the number from each nested array:

const arr = [[37.7], [28.45], [36.38]];

for (let [number] of arr) {
  console.log(number);
}

Sign up to request clarification or add additional context in comments.

2 Comments

when i try this, i got 3 "undefined" messages.
pls, look at the image that i add to the question, i think it will be more clearly
0

From what I can see in the original question so far, the output of this function is actually three different console.log() executions, which leads me to believe that whatever is firing these console.logs is actually running in some sort of loop.

If that is the case, you will not be able to pull just one value out to simply. The screenshot you added only shows the output. Could you please add the code for (or screenshot of) all the related code from setting up or fetching the array, to your console.log? With all that context, I can rewrite my answer to get you the exact answer you are looking for.

2 Comments

I didn't realize that my code have a forEach. The console.log was running 3 times. My bad.
@vavmxd No worries. Glad you figured it out :)
0

Please compare your code with this working example:

let myArray = [37.7, 28.45, 36.38];
console.log(myArray[0]); // outputs 37.7

2 Comments

when I try console.log(myArray[0]),i got: 37.7;28.45;36.38
@gndgn please note that recent updates to the question reveal that the input myArray actually looks like [[37.7], [28.45], [36.38]]. Perhaps you can revise your answer accordingly?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.