简体   繁体   English

访问数组中的对象属性值 - JavaScript

[英]Accessing Object Property Values Within an Array - JavaScript

Learning how to access property values. 学习如何访问属性值。

If I have 如果我有

let object1 = [{name: "HappyHands31"}, {job: "website developer"}, {city: "Chicago"}];

How would I console.log just the value of the second object? 我怎么将console.log只是第二个对象的 Ie "website developer". 即“网站开发者”。

I know how to console.log the entire key-value pair (or object) by using .find() : 我知道如何使用.find()来控制整个键值对(或对象.find()

 let object1 = [{name: "HappyHands31"}, {job: "website developer"}, {city: "Chicago"}]; console.log(object1.find(function(element) { return element.hasOwnProperty("job"); } )); 

But what about just the value of this pair? 但是这对的价值呢?

You can acces items in arrays at given position by their index. 您可以通过索引访问给定位置的数组中的项目。 In javascript indexes of arrays are starting with 0: myArray[0] . 在javascript中,数组的索引从0开始: myArray[0] To access the property of the returned object just use dot-notation: myArray[0].myProperty . 要访问返回对象的属性,只需使用点符号: myArray[0].myProperty

 let object1 = [{name: "HappyHands31"}, {job: "website developer"}, {city: "Chicago"}]; console.log(object1[1].job); 

For your given example this can also achieved by appending the property-name (with dot-notation): 对于您给出的示例,这也可以通过附加属性名称(带点符号)来实现:

 let object1 = [{name: "HappyHands31"}, {job: "website developer"}, {city: "Chicago"}]; console.log(object1.find(function(element) { return element.hasOwnProperty("job"); }).job); 

You can destructure the value: 你可以破坏价值:

 let object1 = [{name: "HappyHands31"}, {job: "website developer"}, {city: "Chicago"}]; const { job: res } = object1.find(({ job }) => job); console.log(res); 

A solution would be to store somewhere the keys of each object and then access the object the usual way. 解决方案是将每个对象的键存储在某处,然后以通常的方式访问对象。

  let object1 = [{name: "HappyHands31"}, {job: "website developer"}, {city: "Chicago"}]; for(let i=0; i<3; i++){ name = Object.keys(object1[i]); console.log(object1[i][name]) } 

In this case I've assumed that you don't know the name of the key you want to access. 在这种情况下,我假设您不知道要访问的密钥的名称。

Just add a property access to the end of it. 只需添加一个属性访问权限即可。

 let object1 = [{name: "HappyHands31"}, {job: "website developer"}, {city: "Chicago"}]; console.log(object1.find(function(element) { return element.hasOwnProperty("job"); }).job); 

First, your object1 is an array of objects, not an object. 首先,您的object1是一个对象数组,而不是一个对象。

Although, there's a way to achieve what you want. 虽然,有一种方法可以达到你想要的效果。

 const fun = (arr, prop) => arr.find((obj) => obj.hasOwnProperty(prop))[prop]; const object1 = [{name: 'HappyHands31'}, {job: 'website developer'}, {city: 'Chicago'}]; const property = 'job'; console.log(fun(object1, property)); 

If the object structure is the same in the array, you can flatten that array of objects like this and call it directly as object keys. 如果数组中的对象结构相同,则可以像这样展平该对象数组,并直接将其作为对象键调用。

 let object1 = [{name: "HappyHands31"}, {job: "website developer"}, {city: "Chicago"}]; let flatten = {} object1.map((currentValue, index) => { let key = Object.keys(currentValue) flatten[key] = currentValue[key] }); console.log(flatten) console.log(flatten.job) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM