This is the code:
var container_list = [
{type: 'editor', id: 't1', placeholder: 'first section'},
{type: 'onlytext', value: 'second section - only text'},
{type: 'editor', id: 't2', placeholder: 'third section'},
{type: 'editor', id: 't3', placeholder: 'fourth section', defvalue: JSON.stringify(formatted_content)}
]
const formatted_content = {"ops":[{"attributes":{"italic":true,"bold":true},"insert":"let's"},{"insert":" write "},{"attributes":{"underline":true},"insert":"something"},{"insert":" wonderful\n"}]}
This works flawlessly:
console.log(JSON.stringify(formatted_content));
But the result of:
console.log(container_list[3].defvalue);
is undefined .
How could i assign a JSON/variable to a property in an object? Thanks!
Swap the sequence you declare these 2 constants. When you declare container_list
, formatted_content
is not yet defined
const formatted_content = {"ops":[{"attributes":{"italic":true,"bold":true},"insert":"let's"},{"insert":" write "},{"attributes":{"underline":true},"insert":"something"},{"insert":" wonderful\n"}]}
var container_list = [
{type: 'editor', id: 't1', placeholder: 'first section'},
{type: 'onlytext', value: 'second section - only text'},
{type: 'editor', id: 't2', placeholder: 'third section'},
{type: 'editor', id: 't3', placeholder: 'fourth section', defvalue: JSON.stringify(formatted_content)}
]
console.log(JSON.stringify(formatted_content));
console.log(container_list[3].defvalue);