Are objects similar to arrays?

Asked

Viewed 161 times

7

Objects are arrays in Javasscript? I have difficulty understanding how this is possible:

var obj = {
   title: 'some value'
}
console.log(obj['title']); // some value

In my conception the use of square brackets is possible only in lists to pass a direct index or have an increment variable to go through all objects.

How is that possible? When I access ['title'] he walks all the Keys (Object.keys) from object to find the requested key or goes straight to "index"?

  • I think this has been asked here before, I need to see if I can find the duplicate.

  • The use of square brackets is a matter of syntax. There’s no such thing as "only possible for such a thing". Each language does it one way. Python, for example, uses this for lists, tuples, dictionaries and strings.

3 answers

8

TL;DR: Yes, generic way - with a few poréns.

Long answer: All prototypical javascript types (Boolean, Number, String, Function, Array, Date and RegExp) are implementations that expand the behavior of Object.

In the case of Array, whose focus is numerical indexing of data, methods such as push() and properties such as length are implemented.

However, you can still use prototypical Object functions such as indexed properties:

var myArray = Array();

myArray['A'] = "Athens";
myArray['B'] = "Berlin";

console(myArray.length); // Resultado será zero

The value of length in the above example is zero because the value was not added using the implementation present in the prototype array - instead, he was added to the object.

Source: https://stackoverflow.com/questions/874205/what-is-the-difference-between-an-array-and-an-object

8


I’ve answered almost every question here.

There is a question dealing with the general issue (I suggest reading to get all the details). In short, the JS objects are actually arrays (membership). So each member of the object in the background is a key to the array. When accesses obj.title, in the background is accessing obj["title"]. It is essentially syntactic sugar.

This is used by several dynamic languages.

The exact implementation of this depends on the engine which is being used, but I can assure you that everyone uses some mechanism of table hash so that the search for the keys takes place in complexity O(1) - constant time. So you can say that it goes straight to the "index", even if you have a previous step.

var meuObjeto = {
    a: 1,
    "b": 2
};
for (var chave in meuObjeto) console.log("key " + chave + "; value " + meuObjeto[chave]);
console.log("tipo: " + typeof(meuObjeto));

I put in the Github for future reference.

  • Very good links, especially the first. Yeah, my question was really how he accessed the attribute, which, as you said, is with hash table. Javascript is a very confusing language, the luck is that there are great articles in the stack over flow to save us, Alice, there is somewhere where I can access all of a certain language ?

  • 1

    The language is confusing and at the same time very interesting. Access tags http://answall.com/tags. Example: http://answall.com/questions/tagged/javascript?sort=votes&pageSize=50 organize as you wish, in the search you can make several filters.

4

No, but they have some similarities. Although you can access properties in the same way, the methods each has are different.

Example:

a = {};
b = [];
console.log(a.forEach); //undefined
console.log(b.forEach); //function forEach(){ ... }

Browser other questions tagged

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