Skip to content

Instantly share code, notes, and snippets.

@andres-mora-vanegas
Created March 27, 2021 18:44
Show Gist options
  • Save andres-mora-vanegas/7f1cfa8d3663ccfc39e469b303541021 to your computer and use it in GitHub Desktop.
Save andres-mora-vanegas/7f1cfa8d3663ccfc39e469b303541021 to your computer and use it in GitHub Desktop.
Function to compare two objects and return keys of first against second
/* example of use
const m ={uno:'1',dos:2,tres:'3',cuatro:'4',cinco:{uno:1,dos:2,tres:3}}
const n = {uno:true,dos:true,tres:true}
console.log(extract(m,n))
// return {uno:'1',dos:2,tres:'3'}
*/
function extract(original, newObj) {
const responseNewObj = {};
Object.keys(original).forEach((e) => {
if (newObj[e] !== undefined) {
responseNewObj[e] = original[e];
}
});
return responseNewObj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment