Unable to bind javascript variable to JSON object key [duplicate] - javascript

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.

Related

JavaScript: Is it possible to declare a variable using another variable's value? [duplicate]

This question already has answers here:
Use dynamic variable names in JavaScript
(19 answers)
Closed 1 year ago.
Here is an example of what I'm trying to accomplish:
const a = 'name';
const ${a} = 1;
The second variable should be:
const name = 1;
Is this possible? Thank you in advance.
Could use an object though, something like
var obj;
var x = "name";
obj[x] = 1;
console.log(obj[x]);
const a = 'name';
eval (a + " = 37");
This will create a variable name with the value 37.
However, I prefer Nobel Eugene's solution as a better approach to the problem.

How to get object values with dynamic variable from json [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 3 years ago.
I want to get the value of an object field from JSON. I have a dynamic variable let check_permission_key = 'ratingscalename'; and have one JSON
let overwrite_key = {
studentimage: 'student',
ratingscalename: 'rating-scale-name',
ratingscale: 'rating-scale-name',
capturesettingset: 'capture-setting-set',
capturesetting: 'capture-setting-set',
eventlog: 'event-log',
goalcategorie: 'goal-category',
userimage: 'user',
datasheetlink: 'datasheet',
datasheetgoal: 'datasheet',
};
let get_value = overwrite_key.check_permission_key;
So I am fetching the data like this, but it is not working can anyone please help me to resolve this issue?
To access the object property value using a variable use Bracket ([]) notation:
let overwrite_key = {'studentimage':'student','ratingscalename':'rating-scale-name','ratingscale':'rating-scale-name','capturesettingset':'capture-setting-set','capturesetting':'capture-setting-set','eventlog':'event-log','goalcategorie':'goal-category','userimage':'user','datasheetlink':'datasheet','datasheetgoal':'datasheet'};
let check_permission_key = 'ratingscalename';
let get_value = overwrite_key[check_permission_key];
console.log(get_value);
Objects in javascript are pretty much arrays with String keys, so you can access a dynamic property like this:
let get_value = overwrite_key[check_permission_key];
You can use eval() function.
let overwrite_key = {
studentimage: 'student',
ratingscalename: 'rating-scale-name',
ratingscale: 'rating-scale-name',
capturesettingset: 'capture-setting-set',
capturesetting: 'capture-setting-set',
eventlog: 'event-log',
goalcategorie: 'goal-category',
userimage: 'user',
datasheetlink: 'datasheet',
datasheetgoal: 'datasheet',
};
let check_permission_key = 'ratingscalename';
let get_value = eval(`overwrite_key.${check_permission_key}`);
console.log(get_value);
JavaScript eval() Function

Node js add dynamic property [duplicate]

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"
}

create an array based on a variables string [duplicate]

This question already has answers here:
Convert string to variable name in JavaScript
(11 answers)
Closed 8 years ago.
I want to pass a string for an array's name to a function, and the function create that array, e.g:
make_array('array_name', data);
function make_array(array_name, data){
array_name = [];
// do stuff
array_name.push(//stuff);
}
I don't want to have to create the array first manually
You can do .
window[array_name] = [];
You can use eval() to do it.
eval("var " + array_name + " = []");
If you just want the function to return an array, there is no need to create it beforehand. You can just do this:
function make_array(data){
var array_name = [];
// do stuff
array_name.push(//stuff);
return array_name;
}
var my_new_array = make_array(data);

How can I convert a string to a variable name in Node.js? [duplicate]

This question already has answers here:
Use variable's value as variable in javascript
(2 answers)
Closed 8 years ago.
//Admin.js
var insertAdminFeed = function(s, id, timestamp){
var admin_att_new_key = '12345';
var admin_att_new_key2 = 'abc';
var admin_att_new_key3 = 'zyzyz';
var s = 'admin_att_new_key';
console.log(global[s]); //should print '12345'
};
exports.insertAdminFeed = insertAdminFeed;
I want to convert a string to a variable in node.js (I have many keys, and I don't want to write if/else statements for all of them) How can I do that?
This is not really possible in JavaScript.
You'd usually use an object literal to achieve similar needs.
var key = 'foo';
obj[key] = 1;
obj['foo'];
To be thorough, it is technically possible in JS using eval. But really, don't do this.
eval("var "+ name + " = 'some value';");
eval("console.log("+ name +")");

Categories

Resources