I have a variable called current_slide which contains a string called "default"
Now I have a object called document.referencemap:
How can I attach the "default" string from current_slide to the object document.referencemap so that I will get
document.referncemap.default ?
What is the best way to attach a string to the object?
At the moment I am calling the object property manually like:
document.referncemap.default.
Can anybody give me a hint so I can solve this issue?
My code looks like this
Because the current slide is always changing i need to load the object from the value of current_slide.
You can use this:
temp0 = document.referencemap[current_slide.slide];
Explanation:
There are two ways to get values from Javascript objects,
using property notation: temp0 = document.referencemap.welcome;
using dictionary notation: temp0 = document.referencemap["welcome"];
This will get the exact same item from the object. Note however that 2. uses a string key. Because of that you can also use a variable that contains a string, in this case: temp0 = document.referencemap[current_slide.slide];
use:
document.referencemap[current_slide] = booBar
make sure current_slide is of type string. Read the Property accessors
docs for more information.
Related
So I'm trying access the property named all like this
let lang = organizationLocales[0].locale;
alertDetails.alertMessage.`${lang}`.all
But sadly, I'm getting an error near that dollar symbol saying that "identifier expected", I just want to access the property in a dynamic way as the variable lang will be changing based on the user input. I'd appreciate any help, thanks in advance.
P.S organizationLocales is an array that has nothing to do with alertDetails object.
You can access to the property of an object through the index accessor []. So if you want to use a string to address a property, you should enclose such string inside square brackets.
You have to think of it like an associative array where each property name is paired to its value, including functions. Since in js syntax, the dot expects the property name statically defined, you cannot compose an identifier like that. But yet you can use a different way to get there without relying on dot.
To better answer to your specific problem, this may be the solution:
let lang = organizationLocales[0].locale;
alertDetails.alertMessage[lang].all
what I basically want to do is this:
variable = 'whatever';
fb.set({ variable : "More Stuff" });
So this will result in an entry that looks like:
whatever: "More Stuff"
Currently it just ends up as
variable: "More Stuff"
Can this be done?
For the latest version of Firebase, use brackets around the variable name:
firebase.database().ref("pathName").set({[variable] : 'MoreStuff'});
Using the other method of setting the index of the variable to the value will create an additional layer in the database structure.
Yes. The code is not working as expected because you are using object literal notation, which is the reason the it keeps the variable name as key, because that is how the notation works.
Solution
foo = {};
foo[variable] = 'more stuff';
fb.set(foo);
Turns out that #randomnamehere 's command didn't quite deliver what I expected, since I wanted to use a accumulated number - or in your case, a named index - inside a reference while still keeping multiple indexes. So the proper command should have actually been
firebase.database().ref(`pathName/${variable}`).set('More Stuff');
His original answer would actually replace the entire reference with a single named index inside it. I know because I learned it the hard way :P
You can create an Empty object and set your variable as it's property and assign a value to it. Please see below:
let object: any = {};
object[variable] = value
I am very new to JSON, have been pestered to get this done.I have to get the value of "extract" as in the given diagram. This diagram is a object diagram of a json. The value 21721040 can be random, thus I do not know the name of the object whose one of the members I seek.
Thus I cannot do something like
query.pages.21721040.extract
So how can I get the value of "extract"?
Out of curiosity should the object whose name is a numeral create an error when I try to access one of its members? Or they just work? For example in this case one of the object's name is "-1".
If I try to access the value "url" after parsing in JS like this:
query.pages.-1.imageinfo.0.url
Will it throw an error?
try this :
query.pages[Object.keys(query.pages)[0]].extract
try query.pages["-1"].imageinfo[0].url.
actually all the fields can be considered as string. so you can write query["pages"]["-1"]["imageinfo"]["0"]["url"]
if you do not know the key, use Object.keys() to find out the keys associated to that object. this key can be any key belong to the object or the object it has been inherited from. to find out only the object's own key user hasOwnProperty
If you don't know the property name, you can iterate over the properties on the object yourself:
for (var key in query.pages) {
if (query.pages.hasOwnProperty(key)) {
console.log(query.pages[key].extract);
}
}
Alternatively, you can use the relatively modern Object.keys() to obtain an array of keys that the object has.
It's super late and my mind is blanking right now, but let's say I have variable filename and it's storing the name of another variable marker. The variable marker is an array and contains the object & property position: new google.maps.LatLng(42.2550,-114.3221).
I've been stupidly trying to access it via filename.position which of course returns undefined, since it's searching the literal filename for a 'position' property that does not exist.
But how could I pull marker.position by using filename? Is there some nifty jQuery trick for, uh, 'resolving' a variable to its contents? I'm brain fried. I know I've done this before.
If it's possible in your script, you can store the data not just in variable, but in a property of some object (usually it's more convenient to use global one).
For example
var myObj = {};
myObj.marker = new google.maps.LatLng(42.2550,-114.3221); // or anything else
Then you will be able to get this property using a variable like this:
myObj[filename].position
In this case i would also recomment to check for myObj[filename] existance using typeof structure, just to make sure such property exists in myObj.
if (typeof myObj[filename] !== "undefined") {
// do something
}
As apsillers noted, you could use global window object for this as well. But if your marker variable was defined inside some other function (i.e. not global), you won't be able to access it with window.marker or window[filename] as it will be out of scope.
Second way is to use eval() function which i'd strongly recommend to avoid.
Try this :
window[filename].position;
I'm trying to use the unobtrusive date picker in an old liferay project (3.6) which I believe is running prototype.js.
I have a call like this:
datePickerController.createDatePicker({formElements:{"elementId":"%d/%m/%Y"}});
made to this:
createDatePicker: function(options) { addDatePicker(options); },
I've not been able to use a variable in place of a hard-coded elementId. I've tried array indexing, dot indexing, string variable, etc. but can't get it to work.
It looks to me like the called function only wants a generally unspecified object, yet if I do one of the above (array, dot, etc.) the browser complains about the bracket (array indexed), the dot (dot indexing), parens or anything other than the expected format.
The underlying called module (addDatePicker) expects formElements so I can't change that.
I don't understand how the browser knows enough to complain about the format of the function's parameter...obviously I'm seriously lacking here!
Pointers greatly appreciated.
e.g.
obj[tag] = 'elementId';
datePickerController.createDatePicker({formElements:{obj[tag]:"%d/%m/%Y"}});
// SCRIPT1003: Expected ':'
You can't put a variable key in an object literal - the syntax requires that the keys be constant values.
You'll need to create the object and fill it, and then pass it:
var obj = {};
var tag = 'elementId';
obj[tag] = "%d/%m/%Y";
// you now have obj = { elementId: "%d/%m/%Y" }
...createDatePicker({ formElements: obj });