这是当我使用Mongoose库在MongoDB上进行查询时获得的JSON对象数组。我得到数组形式的响应。现在,我试图生成自定义的JSON对象,并将其作为响应发送出去
[{
_id: 5 c759f301b164e139f2df980,
Sno: 1,
MaterialName: 'Material1',
MaterialId: '0000000000000000ABCDA001',
LocationName: 'RWH_S1_SZ_AL1',
LocationId: '00000000000000001111A001',
Quantity: '50',
DeliveryLocationName: 'IN4_SEC1',
DeliveryLocationID: '00000000000000003333C001',
PickedUp: 'Yes/No(1/0)',
PickTimeStamp: null,
Delivered: 'Yes/No(1/0)',
DeliveryTimeStamp: null
},
{
_id: 5 c759f301b164e139f2df981,
Sno: 2,
MaterialName: 'Material2',
MaterialId: '0000000000000000ABCDB001',
LocationName: 'RWH_S1_SZ_AL2',
LocationId: '00000000000000001111A001',
Quantity: '10',
DeliveryLocationName: 'IN4_SEC1',
DeliveryLocationID: '00000000000000003333C001',
PickedUp: null,
PickTimeStamp: null,
Delivered: null,
DeliveryTimeStamp: null
},
{
_id: 5 c759f301b164e139f2df982,
Sno: 3,
MaterialName: 'Material3',
MaterialId: '0000000000000000ABCDC001',
LocationName: 'RWH_S1_SZ_AL3',
LocationId: '00000000000000002222B001',
Quantity: '30',
DeliveryLocationName: 'IN4_SEC1',
DeliveryLocationID: '00000000000000003333C001',
PickedUp: null,
PickTimeStamp: null,
Delivered: null,
DeliveryTimeStamp: null
}]
我正在使用此数组作为对使用mongoose的MongoDB查询的响应(resp)。
现在我正尝试通过访问接收到的JSON对象array中的字段来生成自定义JSON对象。因此,当我在下面执行此操作时,这里5不在JSON数组中
for (var i = 0; i <= 5; i++) {
var json = {
LINE1: "MaterialName": resp[i].MaterialName,
"MaterialId": resp[i].MaterialId,
"LocationName": resp[i].LocationName,
"LocationId": resp[i].LocationId,
"Quantity": resp[i].Quantity,
"DeliveryLocationName": resp[i].DeliveryLocationName,
"DeliveryLocationId": resp[i].DeliveryLocationId
}
}
出现类型错误,并指出在LINE1上未定义的属性0是否存在以这种方式访问数组的问题。我现在应该怎么办?请帮助我。
答案 0 :(得分:0)
您的主要问题是此语法无效:
var json = {
LINE1: "foo": "bar", "lala": "lolo"
}
.as-console {background-color:black !important; color:lime;}
您需要将LINE1
键声明为object
,如下所示:
var json = {
LINE1: {"foo": "bar", "lala": "lolo"}
}
console.log(json);
.as-console {background-color:black !important; color:lime;}
因此,您的代码应像这样重新编写:
var json;
for (var i = 0 ; i <= 5 ; i++)
{
json = {
LINE1: {
"MaterialName": resp[i].MaterialName,
"MaterialId": resp[i].MaterialId,
"LocationName": resp[i].LocationName,
"LocationId": resp[i].LocationId,
"Quantity": resp[i].Quantity,
"DeliveryLocationName": resp[i].DeliveryLocationName,
"DeliveryLocationId": resp[i].DeliveryLocationId
}
}
// TODO: Do something with json variable or will be
// overwrite by next iterations.
}
答案 1 :(得分:0)
您可以执行以下操作。假设您在此处共享的大型JSON(来自MongoDB)位于resp变量中。
var resp = [{}]; //This is your large array coming from MongoDB.
function getCustomJsonObject(resp){
var outputArray = [];
for(var i=0; i< resp.length; i++){
var jsonObj = {
"MaterialName": resp[i].MaterialName,
"MaterialId": resp[i].MaterialId,
"LocationName": resp[i].LocationName,
"LocationId": resp[i].LocationId,
"Quantity": resp[i].Quantity,
"DeliveryLocationName": resp[i].DeliveryLocationName,
"DeliveryLocationId": resp[i].DeliveryLocationId
}
outputArray.push(jsonObj);
}
return outputArray;
}
var customObj = getCustomJsonObject(resp);