Foreach only works if the array is inside brackets

Asked

Viewed 144 times

1

Something strange is happening, I’m using foreach to add values directly by the ID, only the strange and the function only works if the array is inside a bracket.

var elements1 = document.getElementsByClassName("rating1");
var array1 = ["9.0", "9.5", "7.1"];

for (var i = 0; i < elements1.length; i++) {
    elements1[i].innerHTML = array1[i];
}

var elements2 = document.getElementsByClassName("rating2");
var array2 = ("9.0", "9.5", "7.1");
for (var i = 0; i < elements2.length; i++) {
    elements2[i].innerHTML = array2[i];
}
<h2>Aqui funciona perfeitamente</h2>
<div class="rating1"></div>
<div class="rating1"></div>
<div class="rating1"></div>
<br>
<h2>Aqui não funciona</h2>
<div class="rating2"></div>
<div class="rating2"></div>
<div class="rating2"></div>

This is just an example of what’s going on.

The problem and the next, I’m using the imdb plugin to capture the qualification of films.

When I make the request the system returns me the data, I believe it is in JSON, all the qualifications that were requested comes together within a value called "rating".

I am using the same code as above, the only thing that changes and I am using the array that comes in the value "rating":

var array1 = rating;

Only instead of working as in the first example, for some reason it is the second example.

2 answers

5

problema.png

I think this picture makes it pretty clear what’s wrong, right?

Your second "array" is not actually an array.

You must be confused with the Python tuple syntax, which does not exist in JS. In Javascript, arrays are in square brackets.

That thing with parentheses that you want to be an array actually only evaluates all the expressions that you put in there separated by , and returns the last. That’s why your items in the second list are 7, . and 1: these are your string characters.

1

To use an array without bracket and as you did in the example where it does not work you need to use . push();

But this one needs to be set before

var array2 = new Array();
array2.push("9.0");
array2.push("9.5");

console.log(array2);
//["9.0", "9.5"]

EDIT Actually using new Array(); is good because you can set the array size

Ex: Array(5);

because it also defines the size of the stack and can prevent stack overflow errors and increase performance

new Array (5) actually will not add five undefined items to the array. Simply add space for five items.

Instantiating an Array this way makes it difficult to rely on array.length for calculations.

EDIT 2

If you are retrieving a JSON object you can use JSON.parse();

var array2 = JSON.parse(objeto_json);

And then you can iterate through your array with a foreach or with a for I advise the for because a foreach will be slower, this has to check a lot before passing the next property/value;

  • 1

    That casts a TypeError: array2 is undefined for me, are you sure you’re right? It seems very strange too...

  • Right, fixed now missing instantiating the array

  • really works the way you said, but only using the data manualment and in the console.log, using the data that comes from the request in JSON does not work, only appears Undefined.

  • 1

    this data that comes from the request may be Object, so it is not working, but I am layman as to js.

  • need to parse with JSON.parse(objeto_json)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.