gpt4 book ai didi

javascript - 如何在node js中添加键和数组对象

转载 作者:行者123 更新时间:2023-12-02 14:55:10 24 4
gpt4 key购买 nike

这是我的结果数组,现在我想添加额外的键和当前键。尝试过。push 不是一个函数

aa[0]['new_key'] = 'blah';    // is not working

我的数组看起来像。

 var aa =  [
{
"main": "56d940bb2b5916181d0906e2",
"current": 6544
}
]

我的原始代码是使用 mongoDB 查询。

connection.modal.find( { 'user_id' : user_id }, { 'abc.def' : true}, function ( err, result ) {

if ( !err ) {

if( result.length ) {

result[0]['abc'].def[0]['new_key'] = 'blah';
response['success'] = true;
response['result'] = result[0]['abc'].def;
response['msg'] = 'data fetch';
res.json(response);
} else {

response['success'] = false;
response['result'] = '';
response['msg'] = 'No record found';
res.json(response);
}
} else {

response['success'] = false;
response['result'] = '';
response['msg'] = 'Error';
res.json(response);
}
});

最佳答案

我也经常遇到同样的问题,我通过使用这个解决了这个问题

connection.modal.find( { 'user_id' : user_id }, { 'abc.def' : true}, function ( err, result ) {
/// ADD THIS LINE /////////
result = JSON.parse(JSON.stringify(result));
....

关于javascript - 如何在node js中添加键和数组对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35866982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com