vavmxd
vavmxd

Reputation: 13

Multiple arrays inside a array in javascript

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

Upvotes: 1

Views: 1958

Answers (3)

Brandon McConnell
Brandon McConnell

Reputation: 6119

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.

Upvotes: 0

Andy
Andy

Reputation: 63524

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);
}

Upvotes: 1

gndgn
gndgn

Reputation: 21

Please compare your code with this working example:

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

Upvotes: 0

Related Questions