The length of an array does not update

Asked

Viewed 83 times

1

In giving console in a array, with an object(1), and lenght from it is obtained the expected return, then update the value of the array, adding a new position, give console in the array and the return says that there are two objects in the array and then to give console in the lenght of him is returned to me as if there were only one object.

Because the console does not return the correct array size when given console right in the length, but returns the right amount when the return is itself array?

Context

This algorithm was to calculate the best distance between several points. The function itself (minus distance) should receive the routes, which is an object with the routes that have not yet been defined, and exclude is an object with the routes that should be removed from the new route because they have already been used.

inserir a descrição da imagem aqui

Update 18/05/2018 I updated the code in order to simplify it and find this problem, which until then, is unsolved.

Project: https://jsfiddle.net/anndreyy/wbzbgj0z/3/

  • What result would you like to receive in these console.logs?

  • In the last line "exclude.lenght" should return 2, conformeo previous console, which returned two positions.

  • I’m sorry, I don’t understand, you can explain to me better?

  • Guy I made a tests here with its function, and the consoles logs return correctly. If I pass an object inside the array in the parameter exclude, shows only 1, if I step 2, shows 2, and so on. I could not replicate the problem in question if I understood correctly.

  • @guastallaigor did you take a look at jsfiddle? La my problem is being replicated on the console. It doesn’t make any sense to me, but I always replicate it. At first I was doing it in Angular, I switched to Youtube and now I’m doing it directly in javascript and always at this point I have this same problem.

  • 1

    Your fiddle doesn’t give any output on the console to me. It seems like it doesn’t even enter the function menor_distancia

  • @bfavaretto this print I took is from the fiddle island. teistei now again and agree, can take another look for kindness?

  • Tell me an updated fiddle URL, that above remains the same thing.

  • @bfavaretto, I think this link is right. Take a look at this video: https://youtu.be/NbrhkFZK9GU https://jsfiddle.net/anndreyy/wbzbgj0z/2/

  • 1

    Look, I think it’s simply a matter of asymptomatism. When you log the entire object, the console only pulls the data of this object, including length, at the time you click on the arrow to open. Then the moment you log into length 1 is earlier than the moment you expand the object on the console and find length 2. Hover your mouse over that [i] that appears next to the expand object arrow on the console. It says exactly that, that the object is calculated at the time of that click.

  • At first moment I imagined it was something like this, after I put a delay, using setTimeout, the return is as expected. However, I wanted to understand why this happens, at what point it happens.

  • I did a test here and it worked correctly

  • On the last console displayed 2? Because here continues 1.

Show 8 more comments

1 answer

0

  • Marcelo, the idea of this passage is to create a array (destinations) with the values rotas.destinos that does not have the values of exclude.

Browser other questions tagged

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