Just use JSON.stringify(object). It's built into Javascript and can therefore also be used within Typescript.
Just use JSON.stringify(object). It's built into Javascript and can therefore also be used within Typescript.
TS gets compiled to JS which then executed. Therefore you have access to all of the objects in the JS runtime. One of those objects is the JSON object. This contains the following methods:
JSON.parse()method parses a JSON string, constructing the JavaScript value or object described by the string.JSON.stringify()method converts a JavaScript object or value to a JSON string.
Example:
const jsonString = '{"employee":{ "name":"John", "age":30, "city":"New York" }}';
const JSobj = JSON.parse(jsonString);
console.log(JSobj);
console.log(typeof JSobj);
const JSON_string = JSON.stringify(JSobj);
console.log(JSON_string);
console.log(typeof JSON_string);
Videos
I hope this example will help for all those who all are working on array of objects
var data_array = [{
"id": "0",
"store": "ABC"
},{
"id":"1",
"store":"XYZ"
}];
console.log(String(data_array[1]["id"]+data_array[1]["store"]));
If you wont aplay join() to Object.
const obj = {one:1, two:2, three:3};
let arr = [];
for(let p in obj)
arr.push(obj[p]);
const str = arr.join(',');
As Jared Smith wrote in the comment
The type of temp.type is { title: { value: string } }. You can't change it to just string, that defeats the whole point of a type system. Make a new, different object that is of the correct type where it's type property is just a string
Just to add more to this. You can create a type that allows reassign like this:
type Temp = {
type: string | {
title: {
value: string;
}
}
}
const temp: Temp = {
type: {
title: {
value: "text"
}
}
}
But there is probably no reason to do this.
typescript prohibits doing that. You need to rethinking your task, because you are doing something wrong.
As says official site "TypeScript is a strongly typed programming language...". among other things it means variable is associated with the type at the time of declaration and the type cannot be changed later.
When you write
let foo = {
title: {
value: "text"
}
};
typescript now knows that foo type is object that consists of one property "title" with value type object that consists of one property "value" with value type string.
type T = {
title: {
value: string
}
}
When you write
foo = "string"
typescript compares two types T and string. Type T is not string. Here you got the type error.
You can use JSON.parse to convert from string to objects:
var x = require('is-valid-glob');
var y = '[]';//'foo/*.js' any user provided string
// this will check if the user has provided an array object if so it
//will do a json.parse to remove the '' and then verify the string for a glob.
x(y[1] !== '['?y:JSON.parse(y));
Try with eval it is used to convert string into equivalent object for an example,
var a="[]";
console.log(a);// this will print "[]" as a string.
console.log(eval(a));// this will print an array object. With 0 length array object.