繁体   English   中英

如何仅将对象的值从 MongoDB 添加到 NodeJS 中的数组?

[英]How to add only values of an objects from MongoDB to an array in NodeJS?

所以这是我的收藏的一个例子:

https://i.stack.imgur.com/E5tES.png

当我运行命令时:

db.Try.find({Type: "Electronic"}, {Brand: 1, _id: 0})

我的结果是:

[
  { Brand: 'Apple' }
  { Brand: 'Dell' }
  { Brand: 'Samsung' }
]

我想把上面的数据放到一个NodeJS数组中,这样我就可以把它显示成HTML的形式。

当我尝试我的 output 时:

[
  { Brand: 'Apple' },
  { Brand: 'Dell' },
  { Brand: 'Samsung' }
]

但预期 output 是:

['Apple','Dell','Samsung']

我怎样才能得到这些结果?

您可以从map拨打 map output:

 const mongodb_output = [ { Brand: 'Apple' }, { Brand: 'Dell' }, { Brand: 'Samsung' } ]; const result = mongodb_output.map(item => item.Brand); console.log(result);

暂无
暂无

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

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