I have a javascript variable, call it temp, that is holding value input by a user. For example,
const temp=10
I want to update an object property to this variable value.
For example, I want to change
var data = [
{
"1": "1.5",
"2": "2",
"subject_input": "null",
"entry": 1
}
]
to
var data = [
{
"1": "1.5",
"2": "2",
"subject_input": value of the javascript variable 'temp',
"entry": 1
}
]
I've tried just including the variable like this:
var data = [
{
"1": "1.5",
"2": "2",
"subject_input": "temp",
"entry": 1
}
]
and this
var data = [
{
"1": "1.5",
"2": "2",
"subject_input": temp,
"entry": 1
}
]
and neither worked. Is there an easy way to do this?
var data = [{"1":"1.5","2":"2","subject_input":"null","entry":1}]
const temp = "temp data";
data = data.map(o => ({...o, subject_input: temp}));
console.log(data);
//or directly assigning temp to subject_input
var data2 = [
{
"1":"1.5",
"2":"2",
"subject_input":temp,
"entry":1
}
];
console.log(data2);