Boky Boky - 2 months ago 7
Javascript Question

Update some object properties

I have something like this :

let test = [
{
name: "Mark",
address: "Some adress",
company: "company name",
age: 21
}
]

let test1 = [
{
name: "Steve",
age: 27
}
]


How can I update test with test1? Thus, what I want to get is :

let test2 = [
{
name: "Steve",
address: "Some adress",
company: "company name",
age: 27
}
]

Answer

You could use Object.assign for it.

The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.

let test = [{ name: "Mark", address: "Some adress", company: "company name", age: 21 }],
    test1 = [{ name: "Steve", age: 27 }];

Object.assign(test[0], test1[0]);

console.log(test);

Comments