Picks the key-value pairs corresponding to the given keys from an object.
Use Array.prototype.reduce() to convert the filtered/picked keys back to an object with the corresponding key-value pairs if the key exists in the object.
constpick=(obj, arr)=>
arr.reduce((acc, curr)=>(curr in obj &&(acc[curr]= obj[curr]), acc),{});
Have you ever wanted to serialize an object but only include certain keys? Turns out JavaScript provides an easy way to do this!
JavaScript, Object · Jul 6, 2021
30 seconds of code uses cookies to provide a high quality user experience and gather anonymized data for statistical analysis of the website's traffic. You can learn more by reading our cookie policy. By clicking Accept you accept their installation.