This is the array of JSON objects I get when I am doing a query on MongoDB using Mongoose library. I am getting a response in the form of an array. Now I am trying to generate the customised JSON object and send it across as a response
[{
_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
}]
I am getting this array as a response(resp) to a MongoDB query using mongoose.
Now I am trying to generate customized JSON objects by accessing the fields from the received JSON objects array.so when I am doing like this below here 5 is no of objects in the JSON array
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
}
}
Type error comes up and says property 0 not defined at LINE1 is there a problem with accessing the array this way. What should I do now? please help me.
Your main problem is that this syntax is not valid:
var json = { LINE1: "foo": "bar", "lala": "lolo" }
.as-console {background-color:black !important; color:lime;}
You need to declare the LINE1
key as an object
, like this:
var json = { LINE1: {"foo": "bar", "lala": "lolo"} } console.log(json);
.as-console {background-color:black !important; color:lime;}
So, your code should be reworked like this:
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.
}
You can do something like this. Assuming the large JSON you shared here (coming from MongoDB) is present in resp variable.
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.