javascript - 如何在node js中从json结果中获取键值

标签 javascript json node.js mongodb

如何从 JSON 数组结果中获取指定的电子邮件及其值,该结果应类似于 =>only( email: abc@gmail.com)

这是我的代码:

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var dbo = db.db("Scream_It");
  var query = { bid_location : 'abbottabad' };
  dbo.collection("bid_placement").find(query).sort({bid_amount:-1}).limit(1).toArray(function(err, result){
    if (err) throw err;
console.log(result);
      // console.log(JSON.stringify(result));
       var string = JSON.stringify(result);
       console.log(string);
        var objectValue = JSON.parse(string);
       // console.log(objectValue);
       console.log(objectValue.email);

这是我在控制台中得到的结果

[ { _id: 5a9f8849fc49ca1ff4aee3dc,
    email: 'abc@gmail.com',
    bid_amount: '200',
    bid_time: '22:22:22:22',
    bid_location: 'abbottabad',
    bid_status: 'false' } ]

最佳答案

这是一个简单的 JavaScript:

var res = [ 
   { _id: '5a9f8849fc49ca1ff4aee3dc',
    email: 'abc@gmail.com',
    bid_amount: '200',
    bid_time: '22:22:22:22',
    bid_location: 'abbottabad',
    bid_status: 'false' },
   { _id: '5a9f8849fc49ca1ff4aee3dd',
    email: 'abcd@gmail.com',
    bid_amount: '200',
    bid_time: '22:22:22:22',
    bid_location: 'abbottabad',
    bid_status: 'false' },
   { _id: '5a9f8849fc49ca1ff4aee3de',
    email: 'abcde@gmail.com',
    bid_amount: '200',
    bid_time: '22:22:22:22',
    bid_location: 'abbottabad',
    bid_status: 'false' }
];

var finalRes = res.map(({email}) => ({email}));
console.log(finalRes);

关于javascript - 如何在node js中从json结果中获取键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49152378/

相关文章:

javascript - 根据按钮值切换div内容

javascript - 从ip获取状态

javascript - 图像渲染问题 React Native

ajax - jQuery Form 插件不会调用 IE 8 中的方法

node.js - 将编译的 typescript 部署到 lambda 时导入模块错误

javascript - 如何在单独的 javascript 文件中获取 HiddenField 值 [asp.net]

java - Jackson ObjectMapper 用方括号包裹每个元素(即使是单个值)

javascript - 在 Node js 下调整图像大小和裁剪的问题

node.js - 使用 res.send() 进行 promise ;

json - Kotlin:将嵌套的JSON对象转换为文字字符串