Javascript does not take changed array before click

Asked

Viewed 127 times

2

I have a click code:

var layerMap = [];

$(document).on('click', '#TipoMapa', function(){

    console.log(layerMap);

});

And I have a code that changes the variable layerMap:

$(document).on('click', '#tipo', function(){
    layerMap['regional'] = layerMap['regional'] || [];
    layerMap['regional'] = 'Alisson';
});

What happens is that I click the button that creates an array in layerMap and after that, I click on the button to give the console.log in the variable layerMap, but the content is always returning me as [], that is, empty. What can be?

  • only to confirm #tipo and then click on the button with id #TipoMapa and at that point the layerMap still this certain void?

  • 1

    Voce is trying to access a property of an array, would not want an object instead of an array?

  • yes, according to JS yes, but if I give a console.log after creating the array in the click function #tipo it returns the array. But I click on the #tipo he gives as empty

  • can be a lot of stuff, can post a little more of your js and html or if possible reproduce your problem in jsfiddle

2 answers

2

When you try to write a variable in the console, what goes to the console is the result of the .toString() what you’ve been through.

For arrays, the result of .toString() is equivalent to the call of .join(), according to the documentation:

Implemented in Javascript 1.8.5 (Firefox 4), and compatible with the 5th version of Ecmascript, the toString() function is generic and can be used on any Object. If the object has a Join() method, it will be called and this value will be returned.

The important thing is that .join() returns the elements of the Array, separated by a comma. But you did not put any element in the Array.

Epa

as well as not putting any element in the Array?

That syntax:

layerMap['regional'] = layerMap['regional'] || [];

It’s object syntax. You added a property to the Array, called "regional", but a property is one thing. An element is another.

So two of a:

  • Or you start using numeric indexes instead of strings, and then layerMap will behave as you expected...
  • Or you make layerMap an object instead of Array, and the properties will appear on the console.

Just do it like this:

var layerMap = {};

-1

Unfortunately, different from other languages like PHP, Python, etc...

Javascript does not accept key named as you did in this example: layerMap['regional'].

He only accepts keys in numbers. If you want named keys, try working with objects, because arrays will not be your best option to key.

I advise looking at w3schools more about Javascript Arrays:

Arrays in javascript

  • The amazing thing is that it creates the right array. Inside the click of the #tipo after creating the array dou a console.log and it returns the right array. But if I click on #TipoMapa he says it’s empty.

  • Javascript accepts named key. It is a way to create and access properties on objects. Inclusive.

  • Precisely because it internally transforms your key into numerical. It does not recognize and tries to convert. When you search, you find nothing. It tries to associate a number to a type of information, type: region 1, state 2, city 3, and so you can go defining.

  • I don’t think @Renan. Look at the Javascript documentation, he accuses a Warning about this.

  • @Dávilandré No. Apart from there is nothing in the documentation about it, you can try it in your favorite environment: 'use strict'; var arr = []; arr['foo'] = 'bar'. Even in strict mode this makes a mistake ;)

  • It actually worked @Renan. Strange is that on the site he talks about it: WARNING !! If you use named Indexes, Javascript will reset the array to a standard Object. After that, some array methods and properties will Produce incorrect Results. I don’t understand.

  • @Dávilandré that warning is from W3schools. W3schools is the worst place in the world to learn how to program (the OS in English talks bad from there), but just like a stopped clock, even the W3S is right at least once a day. Mixing Array and object syntax in the same variable is asking to suffer, because then you lose any guarantee that that array will always behave like Array. The decision of how it will behave is up to those who implement the environment, so you can have different results in different browsers, for example.

Show 2 more comments

Browser other questions tagged

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