Let us suppose I have the following object:
{
foo: "bar"
}
How do I, using javascript, make it:
{
foo: "bar",
bar: "foo"
}
You simply need to assign it a new property using dot notation:
const data = {
foo: "bar"
};
data.bar = 'foo';
console.log(data);
If your property names are variables, use bracket notation instead:
const data = {
foo: "bar"
};
const newProp = 'bar';
data[newProp] = 'foo';
console.log(data);
See the doc on property accessors here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With