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

You can achieve it with destructuring and object rest properties:

const user = {_id: 1234, fistName: 'John', lastName: 'Smith'}
const {_id, ...rest} = user;
console.log(rest);

However rest/rpread properties are still an ecmascript's proposal (currently at stage 3). You can use it with transpilation layer such as babel.


Do it with Array#reduce method with Object.keys method.

const user = {
  _id: 1234,
  fistName: 'John',
  lastName: 'Smith'
};

var res = Object.keys(user).reduce(function(obj, k) {
  if (k != '_id') obj[k] = user[k];
  return obj;
}, {});

console.log(res);


You are taking a shallow copy twice: once with the object literal, and again with Object.assign. So just use the first of the two:

const newUser = {firstName: user.firstName, lastName: user.lastName};