This question already has answers here:
Creating object with dynamic keys [duplicate]
(2 answers)
Closed 5 years ago.
let fileName = "test.c";
let testCase = "Case1";
let test = {};
test.fileName = testCase;
console.log(test)
I need fileName property to be dynamic
What is need is, like below
{
"test.c":"Case1"
}
Can any one help me
test.fileName = testCase;
Won't work in this case. Should be
test[fileName] = testCase;
You can use the ES6 computed property syntax:
{
[fileName]: "Case1"
}
This will be interpreted dynamically as:
{
"test.c": "Case1"
}
Related
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 4 months ago.
I am trying to add a value in place of json object key but it always returns variable name.
My Code:
var projectName='';
let tempArray=[];
let output={};
for(i=0;i<myJsonArray.length;i++){
name = myJsonArray[i].Project;
tempArray.push(myJsonArray[i]);
}
output= {projectName :tempArray};
console.log(JSON.stringify(output));
This returns a JSON as
{"projectName":[{"Day":"MON","Project":"ABC","Billing Rate":"xxx"},{"Day":"TUE","Project":"ABC","Billing Rate":"xyx"}]}
But I need something like this:
{"ABC":[{"Day":"MON","Project":"ABC","Billing Rate":"xxx"},{"Day":"TUE","Project":"ABC","Billing Rate":"xyx"}]}
Can someone help on what I am missing here.
Kind Regards.
You should wrap the project name into [] that would help to make a value become a key
var name = '';
let tempArray = [];
let output = {};
for (i = 0; i < myJsonArray.length; i++) {
name = myJsonArray[i].Project;
tempArray.push(myJsonArray[i]);
}
output = {
[name]: tempArray
};
console.log(JSON.stringify(output));
P/s: I don't see any projectName variable there, so I replace it by name instead.
This question already has answers here:
Length of a JavaScript object
(43 answers)
Closed 2 years ago.
I created an object in JavaScript and stored some information in the object. I searched a lot for solution, but did not find any solution. I want to get the count/length of object.
Javascript Code:
productDetail = new Object();
productDetail["Code"] = "A104";
productDetail["ProductName"] = "Keyboard";
productDetail["UnitPrice"] = 10;
productDetail["Dealer"] = "AMD";
This is what you are looking for:
Object.keys(productDetail).length
You are after the count of the property keys of the object.
const productDetail = new Object();
productDetail["Code"] = "A104";
productDetail["ProductName"] = "Keyboard";
productDetail["UnitPrice"] = 10;
productDetail["Dealer"] = "AMD";
console.log(Object.keys(productDetail).length)
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 4 years ago.
I am working on the following snippet. Why am I not able to get the
let node = 'Em2';
console.log(data.node.c2);
work? As you can see I am able to get data while passing data.Em2.c1 but a dynamic format like this let node = 'Em2'; console.log(data.node.c2); I am getting this error
TypeError: Cannot read property 'c2' of undefined
Code:
var data ={
"Em1": { "c1":"#FFF", "c2":"#EEE" },
"Em2": { "c1":"#DDD", "c2":"#ooo" }
}
let node = 'Em2';
console.log(data.Em2.c1);
console.log(data.node.c2);
var data ={
"Em1": { "c1":"#FFF", "c2":"#EEE" },
"Em2": { "c1":"#DDD", "c2":"#ooo" }
}
let node = 'Em2';
console.log(data.Em2.c1);
console.log(data.node.c2);
Use square braces [] to access object member via variable
var data ={
"Em1": { "c1":"#FFF", "c2":"#EEE" },
"Em2": { "c1":"#DDD", "c2":"#ooo" }
}
let node = 'Em2';
console.log(data.Em2.c1);
console.log(data[node].c2);
Similar question: how to access object property using variable
Instead of console.log(data.node.c2), try console.log(data[node][c2])
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 4 years ago.
i want to pass a string to a javascript function and use it as an attribute like this :
function object (){
this.somevalue = 1;
this.somevalue2 = 1;
this.updateAttribute = function(name,value){
this.{name} = value;
}
}
can it be done ? Thank You .
Yes, you can use [] operator bracket notation:
this[name] = value;
This question already has answers here:
How do I make JavaScript Object using a variable String to define the class name?
(10 answers)
Using a variable value to call an array element
(7 answers)
Closed 8 years ago.
Hard to explain.. I basicly want to do the following:
var doWhat = "speak";
var speak = {
hello: function() { alert: "Hello!"; }
};
// Won't work
doWhat.hello();
It's a bad example, but you should be able to get what I mean.
Is it possible somehow ?
You can use eval(doWhat).hello();. That way the contents of doWhat will be evaluated to the object reference.
You can do something like
var doWhat = {}, str = "speak";
doWhat[str] = {
hello : function() {}
};
doWhat[str].hello();
jsName = 'PageIndexController';
//ST1
eval("if( typeof jsName === 'undefined')alert(111);");
//ST1
eval("if( typeof " + jsName + " === 'undefined')alert(222);");
//ST1 not work
//ST2 work and the output: 222;
//there are two different way using eval, we will get 2 different outcome.