从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