This question already has answers here:
Converting an object to a string
(40 answers)
Closed 8 years ago.
How can I turn [object Object] to a string so I can see what's actually in the array. I tried toString() but that produced the same result as before
It's possible with the method JSON.stringify() :
alert(JSON.stringify(yourObject));
Or with console.log() (visible in your debugger javascript, like Firebug) :
console.log(yourObject);
Live Demo
Related
This question already has answers here:
Is Chrome’s JavaScript console lazy about evaluating objects?
(7 answers)
Weird behavior with objects & console.log [duplicate]
(2 answers)
Closed 11 months ago.
I am trying out JS today and ran into the issue that the values of an array get lost when I convert it. The length of the JSON string is correct.
<script>
console.log(arrayTmpre); // returns the correct array to console
var testtwo = JSON.stringify(arrayTmpre); // should return the array as JSON readable string
console.log(testtwo); // the result looks like this: [null,null,null] The values are missing!
</script>
This is the output in the console
Thanks in advance!
This question already has answers here:
How to get all properties values of a JavaScript Object (without knowing the keys)?
(25 answers)
Closed 1 year ago.
{
pet: {
"0.628": 92694.5,
"8739.836": 96391.94
},
try: {
//same
}
}
When I specify the key, I get the values but i am trying to read all the values without knowing the keys. I have even tried regex, but nothing seems to be working. As you can see i am fairly new. So sorry if this was a stupid question.
console.log(data.pet) // Gives [Object Object]
console.log(data.pet["0.628"])//Gives the value
console.log(data.pet[0])//Gives undefined
I don't see in what context you'd want to access a json object without knowing the keys.
but what you can do is to parse the json file into a javascript object, and call Object.keys() to get the keys of that object
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
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:
Length of a JavaScript object
(43 answers)
Closed 6 years ago.
is there any way in typescript by which I can get length of an object:
Something like this:
say I have an object:
public customer:any={
"name":"Bhushan",
"eid":"879546",
"dept":"IT"
}
Now I am trying to get its length in typescript.
ie. when I am doing customer.length(), I should be able to get value 3 as it has 3 elements.
I tried Object.getOwnPropertyNames(customer.value) but its returning 2 whereas I have 3 elements in my object.
any inputs?
You could try the following:
Object.keys(customer).length
Object.keys(this.customer).length