I want to take that._mCon.usa which have array of object's and put the data like key value
in _mUsa object,In the object instance I've name and path ,I try like following and its not working the _mUsa is not filled with data...any idea what im doing wrong here ?
_mUsa{
},
for(var i = 0; i <= that._mCon.usa.length; i++) {
that._mUsa[that._mCon.usa[i][name]] = that._mUsa[that._mCon.usa[i][path]];
}
this is that._mCon.usa with the name and path properties
Object properties are accessed using .propertyname, so it should be:
that._mUsa[that._mCon.usa[i].name] = that._mUsa[that._mCon.usa[i].path];
You use [name] when the property name is dynamic, and name is a variable containing the property name.
You can use [] with a literal string, e.g. ['name'] and ['path'], but there's little point to that; if the property is known, just use the normal dot notation.
Related
How can I access a element if the name is based on a concatenated value of a string and a number ? I am using twilio and they don't store images not in an array but return a flat file which returns a new element name for each which looks like MediaUrl0, MediaUrl1, MediaUrl2 and so on. Also there is a NumMedia field which would be for 3 attachments 3.
so I use the following
if ( message.NumMedia > 0) {
let i
for (i=0; i < message.NumMedia; i++) {
console.log(`message.MediaUrl${i}`)
}
}
but that does not provide me the actual value but a string of MediaUrl0 since it is not an array I can't use message.MediaUrl[i]
What Jeremy said. Originally you were creating a template string by having the back-tick come before the object that contained the property you were attempting to access. You can't use dot notation obj.prop when accessing properties dynamically.
for (i=0; i < message.NumMedia; i++) {
console.log(message[`MediaUrl${i}`])
}}
How do I use a dynamic parameter name when accessing a variable?
Example:
opener.document.form.namerow_3.value = this.cells[0].innerHTML;
opener.window.form.{varaible}.value=this.cells[0].innerHTML;
In this case, the variable would be namerow_3, which will change based on user selection.
How do I solve the problem?
If I understand your question correctly, you're trying to access a dynamic property of the form object. You can do that by accessing the object like this:
// Base example of how to access namerow_3
opener.document.form.namerow_3.value = this.cells[0].innerHTML;
// Example with static accessor name
opener.document.form["namerow_3"].value = this.cells[0].innerHTML;
// Example with variable
var propName = "namerow_3";
opener.document.form[propName].value = this.cells[0].innerHTML;
Since most regular objects in JavaScript are basically a hashmap, you can usually access an objects property by specifying it's key like you would an array's index (in this case, form["namerow_3"]).
function me(a, b) {
function he(c, d) {
if(!a[c]) {
}
}
}
Please someone explain 'if(!a[c])' for me.
why this square bracket is used here in [c] though it is a parameter. it is not an array obviously.
what does if(!a[c]) make sense? how two parameters are used like this?
There is nothing special about that code.
It is saying, in English, If the property c of a is falsey, then the condition is true.
In JavaScript, bracket notation can be used to access properties of an object or members of an array.
For example, someArray[5] will access the 6th member of the array, while someObject['someProp'] will access someProp of the object someObject.
The argument a is likely an object. The syntax:
if (!a[c])
Checks to see if the property in the variable c on the a object does not have a truthy value. This could be if it was null, false, undefined or any other falsey value.
The bracket notation can be used with property names. So, if you have an object like this:
var x = { name: "John"};
Then, you can access that property like in any of these ways:
x.name
x["name"];
// Or, if the property name is in a variable
var prop = "name";
x[prop]
Your example is using a version of the last of the above three options when the property name is in another Javascript variable.
In javascript, properties can be accessed dynamically using the square-bracket syntax.
Consider the following:
var person = {name:'Sarah'};
console.log(person.name); // logs 'Sarah';
Sometimes, you might want to dynamically access properties of an object, using a variable that holds the name of the property you want to access. The above example could also be written like this:
var person = {name:'Sarah'};
var prop = 'name';
console.log(person[prop]); // logs 'Sarah';
I'm trying to find a key value pair value but i want to pass the key in as an argument but it isn't seeming to work.
function drawPieChart(){
var findData = function(variable){
return dailyLog.find({createdBy: Meteor.user().username}, {fields: {variable: 1}}).fetch()[0].variable;
};
var data = [
{
value: findData(adherence),
color: "#CBDDE7"
}...
I want variable to be passed in twice, once to sort and other to find the value but it is actually looking for the key value pair "variable" which obviously doesn't exist.
How do i make it be seen as the argument?
There are two aspects to this:
Creating the fields object with a property whose name is the value of variable rather than the literal name variable, and
Accessing the resulting field based on the value of the variable (rather than the literal name variable)
Dealing with #2 first because it's easier: In JavaScript, you can access a property on an object either using dot notation and a property name literal (obj.foo), or using brackets notation and a property name string* (obj["foo"]). In the latter case, the string can be the result of any expression, including a variable lookup. So if variable contains "foo", then obj[variable] will get or set the foo property on obj.
Back to #1: For now, you have to create the object you're going to pass as fields first and then assign the property value via brackets notation rather than in an object initializer:
var fields = {};
fields[variable] = 1;
If variable contains "foo", then fields[variable] = 1 sets the foo property on fields to 1.
So putting that all together:
var findData = function(variable){
var fields = {};
fields[variable] = 1;
return dailyLog.find({createdBy: Meteor.user().username}, {fields: fields}).fetch()[0][variable];
// Note ------------------------------------------------------------------------------^--------^
};
In the next version of JavaScript, ECMAScript6 (aka ES6), you'll be able to do #1 with a "computed property name" in the object initializer (and still retrieve it with brackets notation). Perhaps unsurprisingly, computed property names use...brackets!
// ES6 only!!
var findData = function(variable){
return dailyLog.find({createdBy: Meteor.user().username}, {fields: {[variable]: 1}}).fetch()[0].[variable];
// Note ------------------------------------------------------------^--------^ --- and ---------^--------^
};
* Side note: In ES6, brackets notation can be used with things called Symbols as well as strings. It's not relevant to your question, but I said "string" above, and soon that won't be true, so...
I'm making some JS code, where I need to set a variable as a key in a JSON array with Javascript array.push():
var test = 'dd'+questNumb;
window.voiceTestJSON.push({test:{"title":""}, "content":{"date":""}});
Where questNumb is another variable. When doing that code, the part where I just write the test variable it just becomes to the key "test", so I have no idea of getting this to wok. How could it be? Thanks!
If you want variables as keys, you need brackets:
var object = {};
object['dd'+questNumb] = {"title":""};
object["content"] = {"date":""}; //Or object.content, but I used brackets for consistency
window.voiceTestJSON.push(object);
You'd need to do something like this:
var test = "dd" + questNumb,
obj = {content: {date: ""}};
// Add the attribute under the key specified by the 'test' var
obj[test] = {title: ""};
// Put into the Array
window.voiceTestJSON.push(obj);
(First of all, you don't have a JSON array, you have a JavaScript object. JSON is a string representation of data with a syntax that looks like JavaScript's object literal syntax.)
Unfortunately when you use JavaScript's object literal syntax to create an object you can not use variables to set dynamic property names. You have to create the object first and then add the properties using the obj[propName] syntax:
var test = "dd" + questNumb,
myObj = { "content" : {"date":""} };
myObj[test] = {"title" : ""};
window.voiceTestJSON.push(myObj);
{test:{"title":""}, "content":{"date":""}}
this is a JS object. So you are pushing an object into the voiceTestJSON array.
Unlike within JSON, JS Object property names can be written with or without quotes.
What you want to do can be achieved like this:
var test = 'dd'+questNumb;
var newObject = {"content":{"date":""}}; //this part does not need a variable property name
newObject[test] = {"title":""};
This way you are setting the property with the name contained in test to {"title":""}.