node.js 从 Node .js 中的 JSON 数组读取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17794398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:05:58  来源:igfitidea点击:

Read a value from JSON array in Node .js

javascriptarraysnode.js

提问by Fox

I need to access the "State" value from the following array --

我需要从以下数组访问“状态”值——

data =   
{ 
    Images:
    [ 
        {
            ProductCodes: [],
            BlockDeviceMappings: [Object],
            Tags: [],
            ImageId: 'ami-75301c',
            ImageLocation: '54696560/Test Image 3',
            State: 'available',       
            VirtualizationType: 'pavirtul',
            Hypervisor: 'xen' 
        }
    ],
    requestId: '2eb809d3-7f82-4142-b5d1-6af3' 
}

When I try data.Images["State"]or data.Images.StateI get undefined.

当我尝试data.Images["State"]data.Images.State未定义时。

Thanks

谢谢

回答by abu

Images maps to an array which stores objects, so you have to specify the index of the item you want. Try data.images[0]["State"].

图像映射到存储对象的数组,因此您必须指定所需项目的索引。试试 data.images[0]["State"]。

回答by Mahmudul Mannan

You can access like this:

您可以这样访问:

data.Images[0].State

Or even:

甚至:

data.Images[0]['State']

回答by naveen kumar

Access the state with data.image[0].state. Your method was wrong because inside the image, you need an index within the two square bracket, the image property is an array.

使用 访问状态data.image[0].state。你的方法是错误的,因为在 里面image,你需要在两个方括号内有一个索引,图像属性是一个数组。