Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add more information to an object?

Tags:

javascript

Let us suppose I have the following object:

{
    foo: "bar"
}

How do I, using javascript, make it:

{
    foo: "bar",
    bar: "foo"
}
like image 702
Wilm hill Avatar asked Dec 02 '25 05:12

Wilm hill


1 Answers

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.

like image 56
jo_va Avatar answered Dec 03 '25 17:12

jo_va



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!