如何从 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/