简体   繁体   English

无法从json对象数组访问json对象中的字段

[英]unable to access fields in json objects from an array of json objects

This is the array of JSON objects I get when I am doing a query on MongoDB using Mongoose library. 这是我在使用Mongoose库对MongoDB进行查询时获得的JSON对象数组。 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 现在,我尝试生成自定义的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
}]   

I am getting this array as a response(resp) to a MongoDB query using mongoose. 我得到这个数组作为对使用mongoose的MongoDB查询的响应(resp)。

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 现在我试图通过访问接收到的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
    }
}

Type error comes up and says property 0 not defined at LINE1 is there a problem with accessing the array this way. 出现类型错误,并指出在LINE1上未定义的属性0是否存在以这种方式访问​​数组的问题。 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: 您需要将LINE1键声明为一个object ,如下所示:

 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. 假设您在此处共享的大型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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM