This question already has answers here:
Safely turning a JSON string into an object
(28 answers)
Closed 1 year ago.
I'm using typescript and I'm receiving a value with object format, but it's a string.
Lets call it as: myVar.
MyVar have this value (image below) as string.
const string myVar = '{"value":"1"}'
How can I transform this string to object for access your value?
you can use
JSON.parse('{"value": "1"}');
const myVar: string = '{"value":"1"}'
const myVarObj = JSON.parse(myVar)
console.log(myVarObj)
Playground Link
Related
This question already has answers here:
How to check if object property exists with a variable holding the property name?
(11 answers)
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 5 years ago.
I'm attempting to run a list of strings through an object. When I do it individually it works, but when I pass it through as a string it doesn't work. How would I fix this?
// this doesn't work
var a = "IntegrationItem1";
var data = faq.a;
// but this works
var data = faq.IntegrationItem1;
What's causing the first example to not work? Is the variable data seeing it as faq."IntegrationItem1" instead of faq.IntegrationItem1?
You can access properties of the object using it's names:
var a = "IntegrationItem1";
var data = faq[a];
what you need is faq["IntegrationItem1"] => faq[a]
This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 6 years ago.
I have a string like :
$scope.text = '"{\"firstName\":\"John\",\"age\":454 }"';
and I want to convert to js object:
$scope.tmp = {"firstName":"John","age":454 };
Note: JSON.parse() doesn't work!!
It's my sample in codepen
You can do it with angular.fromJson()
in your sample, it would have been $scope.tmp = angular.fromJson($scope.text);
The difference between JSON.Parse() and angular.fromJson, is that angular will check to make sure a string is provided. If it is already an object, it will return the same object.
This question already has answers here:
How to parse JSON easily?
(3 answers)
Closed 6 years ago.
The String I am talking about was initially a part of a JS object like:
var nameVal = "Jacob";
var favNumbersVal = "{\"firstNum\":0, \"secondNum\":1, \"thirdNum\":2}";
var aJSObject = {
"name" = nameVal,
"favNumbers" = favNumbersVal
};
The variable I am interested in is favNumbersVal. Please notice that the starting and ending " around the value of favNumbersVal are the normal double quotes we put around a String whenever we define a String.
The format of the value of favNumbersVal is coming from a library dynamically.
The question is that how do I convert the value of favNumbersVal to a JS object, so that when I later convert sJSObject to JSON using JSON.stringify(), the value of aJSObject becomes a JSON object, and the value of favNumbers becomes a JSON object nested inside the aforementioned JSON object.
Using JSON.parse():
var favNumbersVal = "{\"firstNum\":0, \"secondNum\":1, \"thirdNum\":2}";
console.log(JSON.parse(favNumbersVal));
This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 7 years ago.
Is it possible to access the property of a javascript object using a string?
example: I have a javascript object "obj" that contains a property 'index'.
obj.index = 4;
Now let's say I have a string whose value is the same as a property name of my object:
var str = "index";
Can I use the value of the str variable to access the 'index' property of 'obj'?
Just use the following code:
obj[str]
You can use array Notation (squire bracket notation )
obj[str]
This question already has answers here:
Javascript create variable from its name
(5 answers)
Closed 8 years ago.
I need to run this line of Javascript:
#model.save({ name: #some_element.val() })
But the key, which in this case is name, will change depending on the value of a variable. The variable is a string representation of the key. So in this case, the variable is "name". How can I use the variable to specify the correct key? If I use the variable name directly it is interpreted as the key itself.
var obj = {};
obj[varName] = #some_element.val();
#model.save(obj);