如何获取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 }