-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignObject.js
More file actions
27 lines (25 loc) · 873 Bytes
/
AssignObject.js
File metadata and controls
27 lines (25 loc) · 873 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
* function to update an object. It works like the Object.assign () method
* @param{object} object who is source
* @param{object} object who is target
* @return{object} return the object update
*/
function assignObject(source, target) {
for (var i = 0 ; i < Object.keys(source).length; i++) {
target[Object.keys(source)[i]] = source[Object.keys(source)[i]];
}
return target;
}
/**
* Version ES6
* function to update an object. It works like the Object.assign () method
* @param{object} object who is source
* @param{object} object who is target
* @return{object} return the object update
*/
const assignObject2 = (source, target) => {
return { ...target, source };
};
var source = {color : 'blue'};
var target = {name : 'Romain', city : 'Toulouse'};
console.log(assignObject(source, target)); // return {name:"Romain", city:"Toulouse", color:"blue"}