如何获取 JavaScript 对象的一部分属性?
假设我们的对象如下 −
var details ={ firstName: "John", lastName: "Smith", age:21, subjectName:"JavaScript", marks:89, coutryName:"US", collegeName:"MIT", passoutYear:2018 };
下面的代码仅用于获取一个子集 −
示例
var details ={ firstName: "John", lastName: "Smith", age:21, subjectName:"JavaScript", marks:89, coutryName:"US", collegeName:"MIT", passoutYear:2018 }; var selectedFieldFromObjectDetails = (({ firstName, subjectName,marks,passoutYear }) => ({ firstName, subjectName,marks,passoutYear }))(details); console.log(selectedFieldFromObjectDetails);
要运行上述程序,您需要使用以下命令 −
node fileName.js.
这里,我的文件名是 demo120.js。
输出
这会产生以下输出 −
PS C:\Users\Amit\JavaScript-code> node demo120.js{ firstName: 'John', subjectName: 'JavaScript', marks: 89, passoutYear: 2018 }
广告