Javascript Node.js - 如何检索对象/数组中元素的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30968984/
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-08-23 06:00:18  来源:igfitidea点击:

Node.js - How do I retrieve the value of an element within an object/array

javascriptarraysnode.jsobject

提问by Keydose

I am not sure if I have used the correct terminology within the title, but here is the raw result that I want to retrieve data from.

我不确定我是否在标题中使用了正确的术语,但这是我想从中检索数据的原始结果。

{ items:
   [ { name: 'keydose',
       keys: 69,
       cid: 0,
       $created': '2015-06-21T19:20:38.833Z',
   '   $updated': '2015-06-21T19:20:38.833Z' } ] }

This is created via the use of the twitch-irc-db module with the twitch-irc library for node.js, the output above is received by doing this:

这是通过将 twitch-irc-db 模块与用于 node.js 的 twitch-irc 库一起使用来创建的,通过执行以下操作可以接收到上面的输出:

db.where('users', {name: user.username}).then(function(result) {
    console.log(result);
});

I've tried using console.log(result.items.cid), console.log(result.items.cid[0]) and console.log(result.items.cid.valueOf()) to get the value of cid from the database but I have no clue what else to try, I've been googling for a long time and just can't find anything.

我试过使用 console.log(result.items.cid)、console.log(result.items.cid[0]) 和 console.log(result.items.cid.valueOf()) 来获取 cid 的值从数据库中,但我不知道还能尝试什么,我已经用谷歌搜索了很长时间,但找不到任何东西。

Thanks for your time :)

谢谢你的时间 :)

回答by Cymen

You need to look at the structure. An Object will start with an {and an Array will start with a [. When you see an Object, you can use .propertyNameto access propertyName. For an Array, you will of course need to use an index to choose one of the objects within the Array.

你需要看看结构。对象以 an 开头,{数组以 开头[。当您看到一个对象时,您可以使用.propertyName访问propertyName. 对于数组,您当然需要使用索引来选择数组中的对象之一。

So here is your response object;

所以这是你的响应对象;

{ items:
   [ { name: 'keydose',
       keys: 69,
       cid: 0,
       $created': '2015-06-21T19:20:38.833Z',
       $updated': '2015-06-21T19:20:38.833Z' } ] }

We can do result.items[0]to access the first Object within the array that itemsis a reference to. To get cid, we would use result.items[0].cid.

我们可以result.items[0]访问数组中items作为引用的第一个对象。要获得cid,我们将使用result.items[0].cid.

Typically, if you expect items to be more than one item, you would iterate over them with a forEach, or a forloop or a library-specific method. Using forEach, you can do:

通常,如果您希望项目是多个项目,您可以使用forEachfor循环或特定于库的方法对它们进行迭代。使用forEach,您可以执行以下操作:

result.items.forEach(function(item) {
  console.log(item.cid);
});

回答by reg4in

Hint: result.itemsis an Array (Brackets []mean Array in JS). Google -> javascript arrays

提示:result.items是一个数组(括号[]在 JS 中表示数组)。谷歌 ->javascript arrays

To get the cidof the first item:

要获取cid第一项的 :

result.items[0].cid

To get an array of all cids:

要获取所有cids的数组:

result.items.map(function (item) {
  return item.cid
})

Or if you want to do sth with every item:

或者,如果您想对每个项目都做某事:

result.items.forEach(function (item) {
  // Do stuff!
})