简体   繁体   English

如何从 object 获取数组?

[英]how to get an array from a object?

I need to get a object from a array using node js I tried the below code but it giving the entire objects.我需要使用节点 js 从数组中获取 object 我尝试了下面的代码,但它给出了整个对象。 But I need to print only the name for example only sethu I need to print但我只需要打印名称,例如我需要打印的 sethu

code:代码:

var sethu = [{
    name:'sethu',
    age:24,
    gender:'male'
}]

console.log(sethu)

Output: Output:

[ { name: 'sethu', age: 24, gender: 'male' } ]

Expected output:预期 output:

sethu
console.log(sethu[0].name)

But why do you even define it as an array if you store one element only?但是,如果只存储一个元素,为什么还要将其定义为数组呢?

var sethu = {
    name:'sethu',
    age:24,
    gender:'male'
}

And you can just access it by sethu.name .你可以通过sethu.name访问它。

You need to get by 0th index.so you get like this sethu[0]你需要得到第 0 个索引。所以你会像这样 sethu[0]

to access the name property you do the following way.要访问 name 属性,您可以按照以下方式进行操作。

sethu[0].name sethu[0].name

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

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