Subscribe to a JS object

Asked

Viewed 55 times

0

I wonder if there is any way to subscribe to the content of an object. For example: I have two objects.

object1 {
 name: '';
 address = 'Rua Mario';
 email: '';
}
object 2 {
 name: 'Carlos';
 email: '[email protected]';
}

Doubt occurs here.

this.object1 = this.object2;
console.log(this.object1);

Console output:

object 1 {
 name: 'Carlos';
 email: '[email protected]';
}

What I wanted:

object 1 {
 name: 'Carlos';
 address = 'Rua Mario';
 email: '[email protected]';
}

What’s the easiest way to do that?

1 answer

1


let object1 = {
   name: '',
   address: 'Rua Mario',
   email: '',
}

let object2 = {
    name: 'Carlos',
    email: '[email protected]'
}

const {name, email} = object2;
object1 = {...object1, name, email};

console.log(object1);
  • 1

    Your answer is good, so much so that I gave +1, but, could explain how you did to get the result and not just publish the answer, so it is more didactic for those who asked and for future research!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.