What is the most efficient way to copy some properties from an object in JavaScript?

You can achieve it with a form of destructuring:

const user = { _id: 1234, firstName: 'John', lastName: 'Smith' };
const { _id, ...newUser } = user;
console.debug(newUser); 

However, at the time of writing this answer, the spread (...) syntax is still at the ECMAScript proposal stage (stage 3), so it may not be universally available in practice. You may use it with a “transpilation” layer such as Babel.

Leave a Comment