解构 TypeScript 对象
Destructuring a TypeScript Object
我正在尝试将一个对象分解为另一个对象,我的意思是将对象 A 的属性子集传递给对象 B。
我是这样做的:
const User = new UserImpl();
User.email = user.email;
User.name = user.name;
User.family_name = user.familyName;
User.password = 'Test!';
User.verify_email = true;
User.email_verified = false;
User.blocked = false;
const {
email,
name,
family_name,
password,
verify_email,
email_verified,
blocked,
connection
} = User;
const res_user = {
email,
name,
family_name,
password,
verify_email,
email_verified,
blocked,
connection
};
return res_user;
但是,有没有办法使用 Object.assign() 来做到这一点?或使用箭头 => 函数而不是使用两个变量或分两步完成?
谢谢
如果您只想复制几个属性,您总是可以摆脱解构,只需执行以下操作:
const res_user = {
email: User.email,
name: User.name,
family_name: User.family_name,
password: User.password,
verify_email: User.verify_email,
email_verified: User.email_verified,
blocked: User.blocked,
connection: User.connection,
};
return res_user;
此代码比解构 User
对象更短,只是为了稍后重组它。
或者,如果您只想删除几个属性(假设您知道没有其他属性),您可以使用 rest/spread parameter:
const {
some_prop_i_dont_care_about,
some_other_prop,
...res_user
} = User;
return res_user;
这将创建一个新对象,其中包含每个 属性 除了 上面明确列出的对象,并将该对象分配给变量 res_user
。
如果我是 shallow-copying 我可能会定义的属性列表 a pick()
function using the Pick
type:
const pick = <T, K extends keyof T>(obj: T, ...keys: K[]) =>
keys.reduce((acc, k) => ((acc[k] = obj[k]), acc), {} as Pick<T, K>);
然后假设您有以下接口和对象
interface User {
email: string;
name: string;
familyName: string;
password: string;
verifyEmail: boolean;
emailVerfified: boolean;
blocked: boolean;
}
const u: User = {
email: "luser@example.com",
name: "Larry",
familyName: "User",
password: "th3b1gg3stLUSER",
verifyEmail: true,
emailVerfified: false,
blocked: false
};
console.log(u); // everything
您可以像这样只将您关心的属性复制到一个新对象中:
const v = pick(u, "name", "familyName", "email");
// const v: Pick<User, "name" | "familyName" | "email">
// const v: {name: string, familyName: string, email: string}
console.log(v); // just name, familyName, email
// {name: "Larry", familyName: "User", email: "luser@example.com"}
希望对您有所帮助;祝你好运!
我正在尝试将一个对象分解为另一个对象,我的意思是将对象 A 的属性子集传递给对象 B。 我是这样做的:
const User = new UserImpl();
User.email = user.email;
User.name = user.name;
User.family_name = user.familyName;
User.password = 'Test!';
User.verify_email = true;
User.email_verified = false;
User.blocked = false;
const {
email,
name,
family_name,
password,
verify_email,
email_verified,
blocked,
connection
} = User;
const res_user = {
email,
name,
family_name,
password,
verify_email,
email_verified,
blocked,
connection
};
return res_user;
但是,有没有办法使用 Object.assign() 来做到这一点?或使用箭头 => 函数而不是使用两个变量或分两步完成?
谢谢
如果您只想复制几个属性,您总是可以摆脱解构,只需执行以下操作:
const res_user = {
email: User.email,
name: User.name,
family_name: User.family_name,
password: User.password,
verify_email: User.verify_email,
email_verified: User.email_verified,
blocked: User.blocked,
connection: User.connection,
};
return res_user;
此代码比解构 User
对象更短,只是为了稍后重组它。
或者,如果您只想删除几个属性(假设您知道没有其他属性),您可以使用 rest/spread parameter:
const {
some_prop_i_dont_care_about,
some_other_prop,
...res_user
} = User;
return res_user;
这将创建一个新对象,其中包含每个 属性 除了 上面明确列出的对象,并将该对象分配给变量 res_user
。
如果我是 shallow-copying 我可能会定义的属性列表 a pick()
function using the Pick
type:
const pick = <T, K extends keyof T>(obj: T, ...keys: K[]) =>
keys.reduce((acc, k) => ((acc[k] = obj[k]), acc), {} as Pick<T, K>);
然后假设您有以下接口和对象
interface User {
email: string;
name: string;
familyName: string;
password: string;
verifyEmail: boolean;
emailVerfified: boolean;
blocked: boolean;
}
const u: User = {
email: "luser@example.com",
name: "Larry",
familyName: "User",
password: "th3b1gg3stLUSER",
verifyEmail: true,
emailVerfified: false,
blocked: false
};
console.log(u); // everything
您可以像这样只将您关心的属性复制到一个新对象中:
const v = pick(u, "name", "familyName", "email");
// const v: Pick<User, "name" | "familyName" | "email">
// const v: {name: string, familyName: string, email: string}
console.log(v); // just name, familyName, email
// {name: "Larry", familyName: "User", email: "luser@example.com"}
希望对您有所帮助;祝你好运!