Setting JSON key with indexed array value [duplicate] - javascript

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 4 years ago.
I am trying to populate a JSON object by automatically setting the keys for an JSON object based on the string value of another array. For example,
var test = ["a","b"]
{test[0]:"A"}
However, I get a Syntax error when I do this, if I manually set the value as the string as shown in the third line {"a":"A"} this issue does not happen. I've checked that test[0] does indeed print out "a" and its datatype is a string. Is there any reason why this might be happening?

Try the following:
var test = ["a","b"]
var obj = {
[test[0]]:"A"
};
console.log(obj);

Related

How to get the keyword of a json varible? [duplicate]

This question already has answers here:
How to get all key in JSON object (javascript)
(7 answers)
Closed 2 years ago.
I have a varible, if I print it out, I see this output:
Well, if I'd know the number '118', it would be easy, but in the program where I am using it, I don't know it. So is there any mode to get it without knowing that value?
You could use the Object.keys function to retrieve all keys of your object.
The Object.keys returns an array, you can then access the first element of that array like any other array.
const JSONString = '{"118": {"input1": 6, "input2": 1, "input3": 3}}';
const json = JSON.parse(JSONString);
const keys = Object.keys(json);
console.log(keys[0]);

Accessing variable value by string name [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
How can I access local scope dynamically in javascript?
(4 answers)
Closed 3 years ago.
Issue: I want to access some variables I got in my js-file by a string.
I know there is an option to receive variable value if you have an object and pass the object key name in square brackets to the object, but this is not the case here. Here I have just one comon variable, and I want its value by "sending" a string down.
Example on what I want to achieve:
const constantName = 12345; // Value to access
const stringToUseToGetTheValueOfConstantName = 'constantName';
// I want to know how to get the '12345' by something like this:
const valueFinallyAccessed = `${stringToUseToGetTheValueOfConstantName}`; // I know this returns a string
console.log(valueFinallyAccessed); // 12345
Solution
const valueFinallyAccessed = eval(stringToUseToGetTheValueOfConstantName);
console.log(valueFinallyAccessed); // Prints out the value 12345

Is it possible to create dynamic keys in a object in javascript? [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 5 years ago.
for example I ask the user to enter an input and he enters "key" and I save the value on a variable called INPUT.
Then I want to create an object with this like this.
var obj = {INPUT: "some other input"}; where INPUT = key
I know I can add more values but I need to know the key ahead of time in order to add it. Can I add a new key without knowing what it is?
Yes, with computed property names. Assuming INPUT is an actual variable, simply wrap it in [].
var obj = {[INPUT]: "some other input"};
The long version of doing this would be to use bracket notation when adding your keys.
var obj = {};
obj[INPUT] = "some other input";

how to convert a string to an object in Javascript? [duplicate]

This question already has answers here:
Safely turning a JSON string into an object
(28 answers)
Closed 8 years ago.
I would like to know how to convert the string below to an object in Javascript:
var string = '{"id":"50","actor":"1","subject":"1","object":"18","message":"a ajout\u00e9","status":"unseen"}'
In fact, I would like that after converting the string above to an object, I will be able to access to any element of that object. For example, if I convert the string to an object called "obj" I will be able to access to the value of the element "id" (which is "50") in such way: obj.id
Thanks in advance.
This string is in JSON.
Use
var obj = JSON.parse(string);

Dynamically assign name to object array [duplicate]

This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 10 years ago.
I have a javascript issue.
If I have an object array objAr, the object consists of id,name.
If I was to access objAr[0].id it returns the id value of the first object. What would happen if the object is dynamic and therefore I do not know what it consists of, is there a way to dynamically call the Object attribute?
Currently I am creating another array
var theArr = new Array("id", "name");
and call:
objAr[0].theArr[0] instead of objAr[0].id.
Is there a way to do this better using Javascript?
With Javascript you can call all of the attributes in an object without knowing the keys.
See below:
for(key in objAr[0]) {
console.log(objAr[0][key]);
}
If you just wanted the first attribute you could run:
for(key in objAr[0]) {
var attFirst = objAr[0][key];
break;
}
Additionally for the JS array you could have used square brackets.
var theArr = ["id", "name"];
hope that helps
In javascript you can always use the "array notation" in place of the "dot notation"
So these 2 lines are the same
objAr[0].id
objAr[0]["id"]

Categories

Resources