You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从JavaScript对象中提取指定属性并生成新对象?

提取JavaScript对象指定属性到新对象的简便方法

下面给你两种原生JavaScript的实现方式,都不需要依赖任何外部库:

方法一:ES6对象解构赋值(最简洁)

这是目前最常用的简便写法,直接解构出目标属性,再组装成新对象:

let userInfo = {
    name: 'Ahmed',
    age: 24,
    _id: "34ef5576",
    email: 'ahmed@gmail.com'
};

// 步骤拆分写法,清晰易懂
const { name, _id, email } = userInfo;
const newUserInfo = { name, _id, email };

// 也可以写成一步到位的形式
// const newUserInfo = (({ name, _id, email }) => ({ name, _id, email }))(userInfo);

console.log(newUserInfo);
// 输出:{ name: 'Ahmed', _id: '34ef5576', email: 'ahmed@gmail.com' }

方法二:手动创建新对象(直观易懂)

如果对解构语法不太熟悉,直接手动赋值属性也很简单:

let userInfo = {
    name: 'Ahmed',
    age: 24,
    _id: "34ef5576",
    email: 'ahmed@gmail.com'
};

const newUserInfo = {
    name: userInfo.name,
    _id: userInfo._id,
    email: userInfo.email
};

console.log(newUserInfo);
// 输出同上

内容的提问来源于stack exchange,提问作者Ahmad Benchakhtir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 09:40:01