Total Pageviews

Wednesday 25 September 2019

JavaScript : Copy an object property into another



The easiest way to copy object properties on to another is Object.assign()


Example:
function obj1(){
let target = { a: 1, b: 2 };
return target;
}

function obj2(){
let source = { b: 4, c: 5 };
let new_source = Object.assign(obj1(), source);
new_source.d = "Hello";   //add a new property
console.log(new_source);    //Output : Object { a: 1, b: 4, c: 5, d: " Hello" }
}





Microsoft Logo using flexbox and Reactjs

 <!DOCTYPE html> <html> <head>     <script src="https://unpkg.com/react@18/umd/react.development.js" crossori...