繁体   English   中英

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

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

这是我在使用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是否存在以这种方式访问​​数组的问题。 我现在应该怎么办? 请帮我。

您的主要问题是此语法无效:

 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.
}

你可以做这样的事情。 假设您在此处共享的大型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