Javascript object calling value with dynamic variable - javascript

This is driving me insane. I am trying to grab an object in javascript using a dynamic variable. Static variables appear to work, yet I cant for the life of me see the difference between the static vars I put in and the dynamic vars.
My snippet below better illustrates the issue
console.log(itemId); //E2
console.log(typeof itemId); //string
console.log(typeof 'E2'); //string
console.log(ganttObject.items['E2']); //object [with data]
console.log(ganttObject.items[itemId]); //undefined
The weird thing is that I have extensively used dynamic calling of objects elsewhere in the script, but it is breaking here.
Tested in Safari & Chrome

Works for me in FF4:
Are you sure you don't have a typo or something in your actual code?

I found the answer at last by tracing the value of itemId to the source. Turns out an edit in the html generation added a space after my id.
Moral of the story:
Remember that trailing spaces won't show up on the console window

Related

Object property in Javascript exists in the larger object but is null when accessing just that property

I'm new to JavaScript (and most coding concepts in general) and this feels like there must be some simple solution that I just haven't found yet. I need information from a property of an object (not totally sure if I'm using those terms correctly). The object is created using ArcGIS API for Javascript.
console.log(view.popup);
console.log(view.popup.id);
console.log(view.popup.title);
By logging to the console (using the lines of code shown above) I can see that the properties exist because the below lines are logged to the console (from Line 1).
id: "17e2bf83e50-widget-1"
title: "k0"
I then log just the id property (Line 2) and it prints just the id property. However, if I try to log view.popup.title (Line 3), it logs 'null' to the console. Anything else I try to print out using console.log prints the same value found within view.popup. I just need to be able to use that value stored within view.popup.title and for some reason it seems like the only one where I can see that it's there but can't access it directly?
Edit: This does certainly seem to be the issue commented on by folks below, thanks for those links! I've been trying to do stringify (as suggested in Is Chrome’s JavaScript console lazy about evaluating objects?) but finding that it only finds some of the properties. I also made an attempt at making it wait to try to find it until the property is no longer null, but it's definitely somehow later in the code where that happens (the code just enters an infinite loop until there's a call error).
View is generated using https://developers.arcgis.com/javascript/latest/api-reference/esri-views-SceneView.html#popup (which is why I don't have a full understanding of what goes into it/when and how it makes that view.popup.title property)
view = new SceneView({
viewingMode: "local",
map: scene,
container,
camera,
spatialReference: {
wkid:3089
}
});
After learning here that it's related to view.popup.title not existing yet at the point of logging it, I moved over to the ArcGIS forum to ask about how view is generated and I got an answer that lets me access it! https://community.esri.com/t5/arcgis-api-for-javascript-questions/accessing-title-of-default-popup-created-for-a/m-p/1131210#M75813
view.popup.watch("title", () => {
console.log(view.popup.title);
});

Pass Component Name as Argument and then attach method (not working?)

Maybe I'm not using the right terms/names for my searches but I have not been able to find anything on this topic. I would have thought this would be an easy thing to find. What I'm doing is passing a component name to a function and then trying to use the component's name by attaching a method to it. I have confirmed (via Dev Tools) that the name is correctly being passed but when I use the variable and attach the method the specific request does not work. If I 'hard-code' the exact component name to the method it works perfectly. This makes me think the (various) ways I've been trying to attach the method to the variable name is incorrect (see various attempts below). Can you offer some direction here? Thank you.
Passing to Function ...
const grid_name = "grid_GroupA";
console.log(grid_name); // Shows grid_GroupA
msg_max(newItem, grid_name);
Function (only listing relevant parts)
function msg_max(newItem, grid_target) {
console.log(grid_target); // Shows grid_GroupA
// grid_GroupA.data.add(newItem); // This works ...
// grid_target.data.add(newItem); // This does not work
// (grid_target).data.add(newItem); // This does not work
// [grid_target].data.add(newItem); // This does not work
// grid_target + '.data.add(newItem)'; // This does not work
Thank you ...
Edit ...
In my attempt to provide detail I hope I haven't confused the issue.
In essence, my question is if I can type this exact string
grid_GroupA.data.add(newItem);
and it works for my function, how can I place a variable with the exact string "grid_GroupA" in front of ".data.add(newItem);" and have it seen the same as the line of code that works? Maybe my lack of knowledge here is getting in the way but isn't the line of code that works just a string that is then used to 'find' the object? So, if that assumption is correct, how do I create that same string with the variable? If my assumption is wrong I am a willing learner so I will be all ears. Thank you.
I do not see how grid_target is an object. You are passing grid_name(which is a string) to the function, so grid_target will have no data property, because string doesn't have such a member.
P.S. snake_case is bad option for JavaScript, consider using cameCase instead

"in" operator showing functions in javascript

in my code I was always doing
for(i in vector)...
and it always worked, but the problem is that it somehow changed and now my for shows all the values but also the properties, like "remove" that is a function, and it is breaking my whole code.
I don't know why it suddenly changed, because I didn't do anything and I'm getting crazy with this already.
Do you guys know what is happening with my application?
Another thing is that the code only get this problem on my computer.
If I clone my repository again and try it works for while but then starts the problem again.
Thank you.
The in operator has always had this behaviour. Just check that the property exists directly on the object instead of on the prototype:
for (var i in vector) {
if (vector.hasOwnProperty(i)) {
// Property exists on object
}
}
That should solve your issues.
Tom

google script assining variable bug

I've noticed strange bug while setting a new value to a variable. Unfortunately the Value has been copied to a different variable as well. Do you have an idea what is going on here?
Here are some screen shots while debugging. One just before setting the new value and one just after. You can see how automatically the value has been copied to 2 different variables.
Here is the file if you want to check it yourself:
docs.google.com/spreadsheets/d/17L7KDVteaYUuBE8v5jRRUGBBHa5_Dg6dH0eQ8oDTde4/edit?usp=sharing
Thanks in advance guys
This is not a bug.
Assigning arrays to variables happens by reference (to a memory location) in JavaScript and most other programming languages.
Consider this simplified example
a=[1];
b=a;
b[0]=2;
a is now [2]
To assign a copy you need to create a copy for example using slice
c=a.slice();
c[0]=3;
a will now still be [2] and c will be [3].
So try
red = temp[i].slice();

Javascript json, check all keys for undefined ('null') and set default

Firstoff I'd like to add I've been learning javascript for like only 2 days now. I'm pretty much way ahead of myself with what I'm trying to get but here goes.
I have a json array from which I get data to replace/insert in my page. The first problem I have is that if it comes across an empty ('null') key it will just stop. Will not even try to continu.
document.getElementById("id1")src=json.img1.link;
document.getElementById("id2")src=json.img2.link;
document.getElementById("id3")src=json.img3.link;
json.img2.link is empty ('null' response from json.). javascript will then not replace "id2" but it also won't replace "id3".
I'm now trying to find a solution where it will if nothing else at least set a default.
The script is not continuing executing because it comes to an error --trying to access property link of an undefined object
Try
document.getElementById('id2').src = json.img2 ? json.img2.link : 'defaultLink';
This way your are checking for undefined (ie null) object in img2 and assigning the default value. This assumes that what is not defined (null) is the img2 object.
Actually I don't think your code should work at all, you are missing the. before the src So, try
document.getElementById("id1").src=json.img1.link;
document.getElementById("id2").src=json.img2.link;
document.getElementById("id3").src=json.img3.link;
and let us know if that doesn't solve the problem.
Btw, +points for learning JavaScript and not just straight into jQuery!

Categories

Resources