Say, I have an object:
const user = {_id: 1234, firstName: 'John', lastName: 'Smith'}
I want to create another object without the _id
key:
const newUser = {firstName: 'John', lastName: 'Smith'}
I am using this:
const newUser = Object.assign({}, {firstName: user.firstName, lastName: user.lastName})
Is there a better way to do this?
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.
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};
The most efficient would most likely be a regular loop
const user = {_id: 1234, fistName: 'John', lastName: 'Smith'}; let obj = {}, key; for (key in user) { if ( key !== '_id' ) obj[key] = user[key]; } console.log(obj)
This tiny function will select specific keys to either copy or exclude from copying. exclude take precedence:
function copy(obj, include=[], exclude=[]) { return Object.keys(obj).reduce((target, k) => { if (exclude.length) { if (exclude.indexOf(k) < 0) target[k] = obj[k]; } else if (include.indexOf(k) > -1) target[k] = obj[k]; return target; }, {}); } // let's test it const user = { _id: 1234, firstName: 'John', lastName: 'Smith' }; // both will return the same result but have different uses. console.log( 'include only firstName and lastName:\\n', copy(user, ['firstName', 'lastName']) ); console.log( 'exclude _id:\\n', copy(user, null, ['_id']) );
Go through the object keys, put the wanted property keys in an array and use the Array.prototype.includes()
to copy only these into the new object.
const account = { id: 123456, firstname: "John", lastname: "Doe", login: "john123", site_admin: false, blog: "https://opensource.dancingbear/", email: "john123@example.com", bio: "John ❤️ Open Source", created_at: "2001-01-01T01:30:18Z", updated_at: "2020-02-16T21:09:14Z" }; function selectSomeProperties(account) { return Object.keys(account).reduce(function(obj, k) { if (["id", "email", "created_at"].includes(k)) { obj[k] = account[k]; } return obj; }, {}); } const selectedProperties = selectSomeProperties(account); console.log(JSON.stringify(selectedProperties))
The result:
{"id":123456,"email":"john123@example.com","created_at":"2001-01-01T01:30:18Z"}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.