从 JavaScript 中的对象数组中获取特定值?
假设我们的对象数组如下
const details = [ { employeeFirstName: "John", employeeLastName: "Doe" }, { employeeFirstName: "David", employeeLastName: "Miller" }, { employeeFirstName: "John", employeeLastName: "Smith" } ]
例
以下是获取特定值的代码,本例中获取名字为 “John” − 的值
const details = [ { employeeFirstName: "John", employeeLastName: "Doe" }, { employeeFirstName: "David", employeeLastName: "Miller" }, { employeeFirstName: "John", employeeLastName: "Smith" } ] for (var index = 0; index < details.length; index++) { if (details[index].employeeFirstName === "John") { console.log("FirstName=" + details[index].employeeFirstName + " LastName= " + details[index].employeeLastName); } }
要运行以上程序,你需要使用以下命令 −
node fileName.js.
此处,我的文件名是 demo223.js。
输出
输出如下 −
PS C:\Users\Amit\JavaScript-code> node demo223.js FirstName=John LastName= Doe FirstName=John LastName= Smith
广告