Sobes.tech
Middle

Look at the function in the editor: it takes an array of keys and an object, and uses reduce to copy only the object with the specified keys. What would you name this function?

sobes.tech AI

Answer from AI

A function that takes an array of keys and an object, then uses reduce to create a new object containing only the specified keys is commonly called pick.

This name is widely used in various libraries (for example, Lodash) and accurately reflects the essence — "picking" only the necessary fields from an object.

Example in JavaScript:

function pick(keys, obj) {
  return keys.reduce((acc, key) => {
    if (key in obj) {
      acc[key] = obj[key];
    }
    return acc;
  }, {});
}

This name is intuitively understandable and aligns with common terminology.