Is Array Considered A Collection (Javascript) - javascript

I was looking for the answer to this and I could't find a very good answer but is arrays considered a collection in javascript.
I know that map is considered a collection but wasn't sure about Javascript

it's pretty basic really. It's reading the value in editTextNumber1 and storing it in a variable named edittextNumber1intvalue.
Fun fact about reading text values from controls in android: You can't just "getText(). If you do, you get an object that i believe is a stringbuilder. You have to use the .toString() method of the returned object.
Now on to part 2: Since the value returned is a string, you can't just force that into an integer. It has to be converted. Integer.parseInt will return an integer value if the string passed into it is numeric.
And that's it.

Related

Javascript unable to reference Hashmap using string name

I have multiple Hashmap in my javavscript code and I'm trying to dynamically load the relevant map according to the name passed to the function.
The problem is when I pass the value as string value it actually tries to get the keys of the string rather than the object which it refers to.
This jsfiddle properly illustrates my problem.
Line 13 gives the expected output whereas Line 14 creates keys out of the string name.
Its basically the difference between:
Object.keys(PROP_ONE)
and
Object.keys("PROP_ONE")
While the first is an identifier resolved to an object, the second one is just a string. not more. You may access it using bracket notation due to the fact that its part of window:
Object.keys(window["PROP_ONE"])
Disclaimer:
All in all, dynamic keys should just be used if really neccessary. They make your code slower and more buggy.

When creating a set (unique elements of same type) in JavaScript, what is customary to use as values for a key?

Since JavaScript has no straight concept of a "set", the way I always create an object that acts as a set is to create an associative array where the keys are the elements of the set and the values are true, e.g.
function ToSet ( arr )
{
// return set of
var S = {};
arr.forEach(function (elem) { S[elem] = true; });
return S;
}
Since the trues are dummy data, is there a better value to use? Maybe one that only takes up 1 byte?
Since JavaScript has no straight concept of a "set"...
Javascript does have a Set object as of ES6. You can read about it here on MDN. And, there are multiple polyfills to use something similar in pre-ES6 environments.
If trying to simulate a Set using a plain Javascript object, the key must be a string so you have to find a unique representation of your object that can be used as a string key. For a number, that's the string representation of the number, for a boolean, that's "true" or "false". For an object, you have to create a unique string that represents the object and if the object is presented again you can recreate the same string. There are multiple possibilities for how to do that in pre-ES6. An ES6 set can hold an object directly without making a string representation of it.
Here's a related answer on using a Set-like object in pre-ES6: Mimicking sets in JavaScript?
And, a derivative of that that aims more for the ES6 Set interface here: https://github.com/jfriend00/ES6-Set.
In this ES6-like interface, there's a function called getKey() here that illustrates a strategy for making a unique string key for many types of ES5 variables. You can see the details there.
As for what to set the data to in the Set, the best bet is to set it to the actual value of the data in the set because the key will be forced to be a string so you need to store the actual value somewhere. So, if the value isn't a string itself, then the actual value can be the value in the object.
Since the trues are dummy data, is there a better value to use? Maybe
one that only takes up 1 byte?
As for data size, exactly how Javascript variables are stored is an implementation detail that is not forced by a specification so it can vary from one implementation to another. Because a variable's type has to be part of the value, every variable has to have some sort of universal part of its value. You're not likely to find a value that is smaller than a simple boolean since a type that only has two possible values should be as small as possible already.
But, if you want to be able to tell the difference between a 3 and a "3" stored in your set (e.g. you want them stored as separate items in the set), you will have to add a type modifier to the key so they are stored with separate keys and if you want to be able to get all the values from the set you will need to be able to reconstruct the original value. One simple way to get back to the original value is to store it as the value of the property on the object rather than just true.

What's the point of jQuery.parseJSON(JSON.stringify(someData));

Came across this snippet
var someDataJSON = jQuery.parseJSON(JSON.stringify(someData));
According to MDN the inner "method converts a JavaScript value to a JSON string"; then the outer method "Takes a well-formed JSON string and returns the resulting JavaScript value" per jQuery
If you start with a JS value & end up with a JS value, is this a pointless operation?
Usually that is a trick used to get a by-value copy of an object in javascript. (Since all objects are passed by reference). You can find a more in-depth answer on how to accomplish this, if you're curious, in this stackoverflow post
If someDataJSON is a flat JSON object, this way you get a copy of that object. Since, there is no direct method to copy a javascript object "by value" [and not "by reference"], this trick can be used.
var copyJSONObj = JSON.parse(JSON.stringify(JSONObj))
So, there is some point after all.

Calling toUppercase() on a string variable

I'm a beginner and just successfully trouble-shoot my code. I'm glad that I found it, however it took me a long time. I'm hoping to learn why it happened.
Here's the buggy original code. Assume that the variable [nextAlpha] has already been assigned a string value:
nextAlpha.toUpperCase();
Through some creative testing I was able to determine it was the line causing issues. I thought perhaps it's not actually updating the value of variable [nextAlpha]. I tried this instead, and it worked:
nextAlpha = nextAlpha.toUpperCase();
I've left the rest of my code out, but assume that [var = nextAlpha] has already been declared at the top of my script, which I think means "globally." With that information, I thought it was enough to simply call the method on the variable. Why doesn't this "update" the string to upper case like it does when I go the extra step to (re)assign it to the original [nextAlpha] string?
toUpperCase returns the converted string as a new object - it does not perform the conversion on nextAlpha.
From the Mozilla reference:
The toUpperCase method returns the value of the string converted to uppercase. toUpperCase does not affect the value of the string itself.
reference
In JavaScript, Strings are immutable:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures
Unlike in languages like C, JavaScript strings are immutable. This means that once a string is created, it is not possible to modify it. However, it is still possible to create another string based on an operation on the original string
toUpperCase() is a function (so return a value) not a property (affect the variable itself)

How does String.length work in JavaScript?

I want to know how is the string length of a string calculated in js.
Is is a function call or a class data member.
I want to know what happens when we execute the following code :
a = 'this is a string';
console.log(a.length); // what actually happens at this point?
Also if a do this :
a += ' added something';
console.log(a.length); // at what point is the new length calculated
//and/or updated for the object 'a';
And at last, do I need to store the string length in a temp variable while using a loop over the string or can I directly use the following (which one is faster/processor efficient) :
for(var i=0;i<a.length;i++){
// doing anything here
}
Summing up my question, I want to know the processing behind String.length and which practice is better while looping over strings?
A string is immutable in JavaScript.
a += "somestring" doesn't change the length of a string but makes a new string.
This means there is no "new length", but the length is just part of the definition of the string (more precisely it is stored in the same structure in implementations).
Regarding
for(i=0;i<a.length;i++){ // did you forget the 'var' keyword ?
a not so uncommon practice (if you don't change a) was to optimize it as
for (var i=0, l=a.length; i<l; i++)
in order to avoid the reading of the length but if you compare the performances with modern engines, you'll see this doesn't make the code any faster now.
What you must remember : querying the length of a string is fast because there is no computation. What's a little less fast is building strings (for example with concatenation).
Strings are a primitive type. At least that's what the documentation says. But we can access the length of the string as if we are accessing the property of an object(with the dot notation). Which indicates it's an object, Right?
Turns out, whenever we make a call from the string primitive to some property using the dot notation (for example, say length), the Js engine will take this primitive string and wrap it into an equivalent wrapper object, which is a String object. And then, the .length on that String object returns the length.
Interesting thing to note here is, that when we do something like this, our string still stays the same primitive string during all of this. And a temporary object is created to make our string operation work. Once the required property is fetched, this temporary object is deleted from the memory.
Hope this gives some high level understanding.
I'm answering your first question.
I'm also curious about this puzzle so I did some search myself, ended up finding -
Based on String documentation from Mozilla:
String literals (denoted by double or single quotes) and strings
returned from String calls in a non-constructor context (i.e., without
using the new keyword) are primitive strings. JavaScript automatically
converts primitives to String objects, so that it's possible to use
String object methods for primitive strings. In contexts where a
method is to be invoked on a primitive string or a property lookup
occurs, JavaScript will automatically wrap the string primitive and
call the method or perform the property lookup.
So as I understand, when you use somestring.length, the primitive string will first be wrapped as a String object, and then since the object has its property length, so it's just a internal method call to access and return.

Categories

Resources