How do I reverse() an associative array (object)? [duplicate] - javascript

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Loop through associative array in reverse
I have a key-value pair javascript object.
How do I reverse() it? or loop it starting from the bottom.
jQuery available.

There is no hard specification or guarantee of determining the order of properties in a Javascript object. Most browsers go from what was added first to last, but as Elements order in a "for (… in …)" loop
describes, Chrome and Opera do not.
If order is important to you, then you must use an array, or have an array that lists the order in which the properties should be accessed when looping (from either direction).

Related

What is the time complexity of removing/accessing property in an object in javascript [duplicate]

This question already has answers here:
Complexity of accessing data in an object
(2 answers)
Closed 2 months ago.
I was watching a youtube tutorial in DS&A (using JS) and he mentioned that the time complexity of accessing or removing property in an object is constant?
how is that possible shouldn't we search for this property first and then delete it or access it. so, it should be linear!
JavaScript objects are actually hash tables under the hood. (See https://en.wikipedia.org/wiki/Hash_table for more info on how hash tables work.)
In simple terms, in a hash table, the key itself directly correlates to a number when put through the hash table's hashing function, which number is (or correlates to) an address. Thus, JavaScript can (typically) directly use the key name to access that number and/or address. This has runtime complexity O(1) because the key's string (if it's a string) can be decoded into an address without any iteration over the other keys in the object.

Are arrays objects in JavaScript? [duplicate]

This question already has answers here:
Are Javascript arrays primitives? Strings? Objects?
(7 answers)
Closed 3 years ago.
I have had discussions with seasoned programmers who do not think arrays are objects; they think that objects and arrays are of separate types of the data structure category. But, as far as I understand, objects and arrays are of the same type.
If you were to use the unary typeof operator to evaluate a binding storing an array, the JavaScript interpreter will return the string "object." Moreover, you can use the Object.keys() method on a binding containing an array to get the property names of the array object.
When studying programming, I see authors use the phrase "objects and arrays," as if these were separate entities, which adds to the confusion. I go to W3Schools, and in the section "Data Types," they list arrays and objects as if they are not of the same data type.
As far as I understand it, objects and arrays are the same, except array objects are structured different than objects delimited by braces. Perhaps I'm missing something and can be enlightened.
Are arrays objects? If so, shouldn't they be listed as the same type, and shouldn't the phrase be "arrays and other objects"? If not, why not?
(Edit: I know the question of if objects and arrays are the same has been asked, but I was also asking if there should be a change in how arrays and objects are discussed, so people don't think that arrays are not objects, since programming is not the easiest subject to digest.)
Arrays are indeed a type of object.
console.log([] instanceof Object);
I go to W3Schools, and in the section "Data Types," they list arrays and objects as if they are not of the same data type
W3Schools is not a particularly trustworthy source. That said, when discussing organization of data, it's pretty common to talk about arrays (an ordered collection of values) as distinct from objects (a usually-unordered collection of key-value pairs), despite the fact that one is a subtype of the other, because the fact that arrays inherit from Object.prototype often isn't something one needs to consider when organizing / sorting through data.
If one tries to use any of the Object methods on an array, it's certainly essential to remember that Array inherits from Object, but usually that's not necessary - often, one will be only be using array methods instead (like forEach, find, includes, etc).

JavaScript Objects built on arrays [duplicate]

This question already has answers here:
Why can I add named properties to an array as if it were an object?
(8 answers)
Difference between array and object in javascript? or Array Vs Object
(4 answers)
Associative array versus object in JavaScript
(7 answers)
Closed 4 years ago.
From my readings, in JavaScript:
Objects = Hash Tables, which are build on Arrays. However, it is commonly said that Arrays are Objects in JS. How are these two concepts reconciled?
Objects are not built on arrays. Objects have their own optimizations.
In general:
Objects are for "structs", structures of predictable "shape" and keys known in advance (even though they can be used with dynamic keys, you should use Maps for that. See below).
Arrays are for lists (and queues, and stacks), structures where the keys are numbers, or where the order of elements matters. Arrays are "special" objects, not the other way around. (You can put string-based properties on an array, just like any object. Please don't do that though).
Maps are for hash tables/dictionaries, structures where the keys are dynamic and not known in advance.

Does the length of an object's property names impact memory usage? [duplicate]

This question already has an answer here:
Do browser engines compress keynames in large arrays of reoccurring objects?
(1 answer)
Closed 5 years ago.
I have array of objects containing around 200000 records. The objects have long key names. Will shorter property names consume less memory? Will it be enough to make a difference in performance?
If I prepare my data like:
[{"dTN":"datatype value","dTR":"dataType range"},....]
instead of:
[{"dataTypeName":"datatype value","dataTypeRange":"dataType range"},....]
how it will effect memory usage in browsers?
Can anyone please explain how javascript handles object property names?
You can check using window.performance.memory in Chrome.
If you have many repeating strings I believe they will be optimized, this completely depends on the engine of course. If the keys are all different you might see an increase.

When would you use an ES6 Map over an Object? [duplicate]

This question already has answers here:
Map vs Object in JavaScript
(15 answers)
Closed 7 years ago.
I've been looking at the new ES6 docs on MDN and I can't find a real world use for the Map object where a normal object wouldn't work. Does anyone have any use cases for Maps and explain why an object wouldn't work in that scenario?
MDN lists a number of important differences:
An Object has a prototype, so there are default keys in the map. [editor: the good old hasOwnProperty issue]
The keys of an Object are Strings, where they can be any value for a Map.
You can get the size of a Map easily while you have to manually keep track of size for an Object.
A Map iterates its elements in insertion order, whereas iteration order is not specified for Objects.
So a Map is finally an insert-ordered key-value store for Javascript, which additionally allows mapping any value to any value, instead of restricting keys to be strings. This can greatly simplify some code where ordering is important, or where objects or other complex data types need to be associated with other data.

Categories

Resources