jquery console.log and alert different value - javascript

I realy cant understand javascript. Maybe someone can explain me the difference:
validationErrors[value.element.name] = value.method;
console.log(validationErrors);
alert(validationErrors);
console.log(validationErrors) returns well formed array with values, and alert(validationErrors) returns empty array. Why?

The console is more of a debugging environment and can understand the JS objects that are passed in the log function.
Alert on the other hand is a dialog box and will coerce its arguments into string values. Which is why the output is not as well formatted as the console.
Here is a little snippet of what is actually happening in the alert box.
var validationErrors = [ 2, 3, 4 ];
console.log(toString(validationErrors));
Output >> "[object Window]"
It's also best practice to use the console for logging purposes and not the alert box.

you can try this
alert(JSON.stringify(validationErrors));

Alert have some limit according to browser it varies but most You'd probably be best staying under 1000 characters though, as many browsers seem to begin to truncate after 999.
And if you are giving object in alert it will never reflect the values so prefer console for object and other data type can be used in alert but its not a good practice.
console give proper view of data like object in array can be studied easily in console.

Alert is used to print messages, which means it is used to display string values. When you pass anything other than string to alert, it calls .toString function of it and prints the output.
Now why did you get a blank string?
An array is supposed to have indexed values only. So you can do array[index] = "bla bla". But when you do array["something"] = "something else", it adds a property to that array (since arrays are also objects in Javascript).
According to MDN
The toString() method returns a string representing the specified array and its elements.
In simple, it will loop over length of array and join all elements with ,(comma)
But you do not have elements in it. You have set properties. So length is 0 and hence it returns ""
Following is a simulation
var a = [];
a["test"] = "foo";
console.log(a);
console.log(a.toString());
alert({})
Reference
Alert
Array.prototype.toString

All objects in Javascript are passed by reference. So when you passed something to console.log() and it would be changed in code further, in console you will see changed value. On other hand, alert() displays message box and stops code execution, so in message box you see values exactly as they are at alert()'s call time.
For debugging purposes, you may want to use browser's debugger - I recommend Chrome Dev tools. There is free course from codeschool.com to help you discover such great tool: https://www.codeschool.com/courses/discover-devtools

Related

TypeScript: empty array is initialized with values, and those values cannot be changed

Now, this might just be me not knowing some basics of how TypeScript works, as I am pretty inexperienced with it, but all my attempts to find a solution to this issue have so far failed.
In one of my functions I am trying to get an item-by-item disjunction of two boolean arrays. However, when I am initializing an empty array to then fill with new values, something strange happens.
let res = new Array<boolean>();
console.log(res)
Output of the freshly initialized array "res"
As you can see, while array is depicted as empty in its collapsed form, opening it for more info reveals that it actually already has 5 "false" values in it. (note: it is not always 5 falses, this is just an example)
When the function then starts pushing new values into it, while those values are depicted correctly in the array's preview, in reality there are still the same five "false" values inside it, and those are not being changed in any way. Furthermore, logging specific items from this array returns correct values
res.push(a[i] || b[i])
console.log("disjunction at position " + i.toString())
console.log(a[i])
console.log(b[i])
console.log(res[i])
console.log(res)
Console output
This function then returns a new boolean array to be pushed into an array of arrays. When I am logging this array of arrays, it depicts not arrays of values the function filled them with, but instead those weird values that have been put there with initialization. This does not seem to affect the first array (and only that one).
Array of arrays
As I said, I might just miss some crucial information about how arrays in TS/JS work. Right now I am a bit lost, so any help would be appreciated!
Turns out it was just me not knowing how dev console works. The big array of arrays is being filtered after being filled up, and because console shows all the arrays in real time, filter causes it to retrospectively depict them as full of falses. Temporarily removing filter proved this hypothesis.

How to check wether or not a value is contained in an array when the value type is ObjectId?

I fetch an array that is stored in mongoose with ObjectIds. After I output this array through the console I get the following output:
["5a5f7199105cc908e874d74b","5a5f9199105cc908e874d74b"]
At the same time fetch a different object from mongoose. If I output the ObjectId of this object through the console (object.id) I get the following output:
5a5f7199105cc908e874d74b
I try to check whether the value is contained in the array or not. My code looks like this:
myArray.includes(myObject.id)
and I expect to have the result of true. Unfortunately, I get false as a result even though The value is in the array. Maybe it has to deal with the OBject Type? I really do not know how to solve this. Thanks.
Because in case of Array ["5a5f7199105cc908e874d74b","5a5f9199105cc908e874d74b"] - typeof values is String, so you operate with strings, while when you operate with object property(myObject.id) - you, probably, operate with ObjectID, which is default for latest MongoDBs. Anyway, we need to see your code and know your DB version etc. Easiest way to check is run code "typeof myObject.id", and if you see not "string" - this is an answer to your question.

Javascript map string lookup. Can't find key even though key is in object

I must be missing something pretty standard here.
I have a map mapping string to object. What I write out:
console.log(playerMap, this.id(), playerMap[this.id()]);
What it prints out:
this does not make a lot of sense to me since the key seems to be right there.
Fiddle
Based on your Fiddle, apparently your playerMap is not yet initialised when you new FeedViewModel(), if you change your console.log to
console.log(Object.keys(playerMap).length, this.id(), playerMap[this.id()]);
you will see the object is empty
> 0 "123243df6" undefined
It's always confused when you console.log object because the object displayed is only resolved when you expand the > in the console. It is not the state of the object when you console.log'd the object.

Different console.log(variable) outcomes for exactly the same variable

My issue is that logging exactly the same variable value, but accessing it in a different way, yields two different results.
console.log(car[1].wheels.radius)
logs the integer 20 to the console, since 20 is the value assigned to car[1].wheels.radius.
Now, if I log the whole object:
console.log(car[1])
and access the element radius manually in the console, I can see that its value is 'NaN'.
Same happens when I use car[1].wheels.radius in a calculation, for instance 3.14*car[1].wheels.radius returns NaN, even though this is a multiplication of 3.14, a number, and car[1].wheels.radius, also a number, so it should return a number.
Anyone knows what the issue might be?
OK to actually answer the question in the title about why you get different results when logging a variable -
If a primitive is sent to the log then it is immediately displayed with the value so in this case when you send car[1].wheels.radius it does have the value 20.
However when you log an object,car[1], the values for the attributes are not actually shown until you inspect the object in the console (if you are using chrome there is a little blue (i) which explains this). Think of it like you are logging the reference, the actually objects state will not be shown until you expand/inspect that reference.
So you are performing some calculation on car[1].wheels.radius which changes it from 20 to NaN at some point which explains the difference.
The cause of that problem is unknown from the amount of code you have shown. Use the debugger to step through your code and look to see when this value changes.

JSON.parse not evaluating JSON strings properly

I am using JSON.parse to parse this JSON string
[{"created_at":"2012-01-24T22:36:21Z","name":"joe","age":42,"updated_at":"2012-01-24T22:36:21Z"}]
However I am simply getting this result as the output:
[object Object]
Which shouldn't be the result. I am using this within the Cappuccino framework. Does anyone know what I am doing wrong here?
[object Object] is what objects display when you call toString on them. It looks like you're taking your result and trying to call obj.toString()
Also, your JSON is an array with one element in it, so to verify that your result is correct, you can access the name property on the [0] index:
obj[0].name // should be "joe".
var text = '[{"created_at":"2012-01-24T22:36:21Z","name":"joe","age":42,"updated_at":"2012-01-24T22:36:21Z"}]';
var obj = JSON.parse(text);
alert(obj[0].name); //alerts joe
DEMO
Or get rid of the array, since it's not really doing much
var text = '{"created_at":"2012-01-24T22:36:21Z","name":"joe","age":42,"updated_at":"2012-01-24T22:36:21Z"}';
var obj = JSON.parse(text);
alert(obj.name); //still joe
DEMO
This is an array because it's in square brackets - [] - remove these and it should work...
Even though this is 'syntactically' correct the parser sees this as an array (which is a type of object) but won't do the work on it the way you'd expect.
Also for future reference:
Try to lint it, and see if your syntax is messed up: http://jsonlint.com/
This is an old subject, but nonetheless, I spent hours trying to figure out what was going on. So, hopefully this will help someone else in the future.
My issue was setting up a simple ajax call, and doing something with the resultset from said ajax call. However, no matter what I did, I couldn't get the json resultset to an object.
I ended up stepping through everything in the debugger window and noticed old code that was no longer active was showing up on the sidebar (dom detail). So, my data had been cached. I cleared the cache, and boom! Everything worked.

Categories

Resources