Let's say I have this obj:
let userInfo = {
name: 'Ahmed',
age: 24,
_id: "34ef5576",
email: '[email protected]'
}
how can i extract the properties name, _id, and email. And put them in a new Object? thanks.
So I want a simple way to solve this without any external libraries..!
I find it:
let's say we have this Object, The original Object that we want to extract from its specific Fields:
const parentObj = { name: "ahmed", age: 22, country: "morocco" };
Now we are creating a function that creates for us the extract object:
function pickFromObj(originalObject, arrayOfWantedProperties){
let resultObj = {};
for(property in originalObject){
if(arrayOfWantedProperties.includes(property)){
resultObj[property] = originalObject[property]
}
}
return resultObj;
}
Finally Create the extract Object:
let childObj = pickFromObj(parentObj, ['name', 'age']);