Is there a way to clone an object only from specified properties. The new object must contain only properties specified in the array
Asked
Active
Viewed 36 times
1 Answers
0
function cloneWithProperties(obj, props) {
let retObj = {};
if (!props) { // if no props are specified
throw new Error('Props must be specified');
}
props.forEach(function(element) {
retObj[element] = obj[element];
}, this);
return retObj;
}
let old = {
name: 'clinton',
age: 10,
salary: 5000
}
let n = cloneWithProperties(old, ['name']);
console.log(n); // { name: 'clinton' }

Clinton Yeboah
- 126
- 3
- 9