Two-dimensional array causes elements to become NaN.

Asked 1 years ago, Updated 1 years ago, 26 views

var array=[
  [_src*_src,_mid*_mid,_dst*_dst],
  [_src,_mid,_dst],
  [1, 1, 1],
  [0,top_z,0]
];
for (key in array) {
  console.log (array [key]);
}
console.log(array);

In the above code, the console.log in the for loop is displayed as an array of numbers, but in the last console.log, all elements are NaN.
Also, _src, _mid, _dst, top_z are all numeric values.
I'd like to use array as a two-dimensional array of numbers.

If you know the cause and solution of this problem, please let me know.

javascript

2022-09-30 19:16

2 Answers

The array itself is a two-dimensional array in the description of the question.
Therefore, you can use array as a two-dimensional array of numbers.

I can only say that the reason why I can't see it in console.log is because of the environment in which I did this.
While there is no background in implementing this feature,
Different browser types and versions work differently.

Basically, if console.log(x), the string returned from x is
This is the output to the console.
However, in most cases, you have added a unique feature that parses and displays x.

The questionnaire does not have an environment, so it cannot be identified, but
If you look at how it works, expand the array only once and enter it from array[n]
Translating array object return values to NaN by interpreting themselves.

If you want to change this view, change your browser or its version.
You may want to include the function to return the string yourself.


2022-09-30 19:16

When array was declared, there was already an answer to the fact that it was a two-dimensional array, so from a different point of view...

_src, _mid, _dst, top_z are all numbers

It says ... but I don't think so.
You are squared the number, but are the numbers coming in there?
If the operation fails, such as multiplying by undefined, it becomes NaN.
The second element seems to contain the original, so
Why don't you check if the price has reached there?

By the way, depending on the browser, Chrome treats it as 0 whether it is empty or null.
Also, if it's a string type, they can cast it on their own.
sample code


2022-09-30 19:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.