如何获取对象中的子数组值

时间:2017-05-02 12:05:39

标签: javascript angularjs mongodb

我在mongodb中使用嵌套集合。我从mongodb通过angularjs获取所有数据。

我的代码如下所示

       $http.get('/Unitget').then(function (response) {
        UnitDetails = $scope.UnitDetails  = response.data;
        UnitDetails.forEach(function (element) {
         var c = element.Beds_Details;  // test purpose
           type1_Bed += element.Beds_Details[0].type1_Bed ;
           type2_Bed += element.Beds_Details[0].type2_Bed ;
           type3_Bed += element.Beds_Details[0].type3_Bed ;
           type4_Bed += element.Beds_Details[0].type4_Bed ;
           type5_Bed += element.Beds_Details[0].type5_Bed ;
    }
    }

但是如果我删除了 “[0]” ,那么element.Beds_Details[0].type1_Bed没有获得任何值,那么它显示undefined。如果我将 Beds_Details 分配给另一个变量,它就会正确地指定为一个对象。

我的收藏样本记录如下

    "UnitId": "59225fd86ea6863028aba8b1",    //unique id
    "UnitName": "Hospital",
    "UnitIcon": "./upload/file-1493206621849.png",
    "FloorId": "592254b16ea6863028aba7e2",   // unique id of another collection
    "Material_Stream_Flow_Details": [
        {
            "Stream_Flow_DetailsID": "3",
            "fa": "590042c06ea6863028aba777", 
            "fb": "590042c06ea6863028aba777",
            "fc": "590042c06ea6863028aba777",
            "fd": "590042c06ea6863028aba777",
            "fe": "590042c06ea6863028aba777",
            "ff": "590042c06ea6863028aba777",
            "_id": ObjectId("59006a8d6ea6863028aba8cb")
        }
    ],
    "Destination_Details": [
        {
            "Elevator_ID": "2",
            "Elevator": "590042c06ea6863028aba777",
            "One_way": 42.6,
            "Round_Trip": 21.3,
            "_id": ObjectId("59006a8d6ea6863028aba8cc")
        }
    ],
    "Beds_Details": [
        {
            "Beds_DetailsID": "1",
            "type1_Bed": 0,
            "type2_Bed": 0,
            "type3_Bed": 0,
            "type4_Bed": 0,
            "type5_Bed": 0,
            "type6_Bed": 0,
            "_id": ObjectId("59006a8d6ea6863028aba8cd")
        }
    ]

如何在嵌套集合中获取值。

1 个答案:

答案 0 :(得分:0)

您可以迭代Beds_Details数组并将keyvalues推送到另一个数组中。请根据您的要求修改数组。我使用以下架构创建新对象:

{
    type: key,
    value: value
}

我使用JQuery $.each方法进行迭代。 Angular有类似的方法:

var arr = [];
angular.forEach(json, function(value, key) {
    arr.push({
        type: key,
        value: value
    });
});

演示 https://jsfiddle.net/sumitridhal/j3m2h4r0/6/

<强> JS

var jsonObj ={ "UnitId": "59225fd86ea6863028aba8b1",    
    "UnitName": "Hospital",
    "UnitIcon": "./upload/file-1493206621849.png",
    "FloorId": "592254b16ea6863028aba7e2",   
    "Material_Stream_Flow_Details": [
        {
            "Stream_Flow_DetailsID": "3",
            "fa": "590042c06ea6863028aba777", 
            "fb": "590042c06ea6863028aba777",
            "fc": "590042c06ea6863028aba777",
            "fd": "590042c06ea6863028aba777",
            "fe": "590042c06ea6863028aba777",
            "ff": "590042c06ea6863028aba777"
        }
    ],
    "Destination_Details": [
        {
            "Elevator_ID": "2",
            "Elevator": "590042c06ea6863028aba777",
            "One_way": 42.6,
            "Round_Trip": 21.3
        }
    ],
    "Beds_Details": [
        {
            "Beds_DetailsID": "1",
            "type1_Bed": 0,
            "type2_Bed": 0,
            "type3_Bed": 0,
            "type4_Bed": 0,
            "type5_Bed": 0,
            "type6_Bed": 0,
            "_id": "59006a8d6ea6863028aba8cd"
        }
    ]};

let json = jsonObj.Beds_Details[0];
delete json['Beds_DetailsID'];
delete json['_id']; // Removes Beds_DetailsID & _id

var arr = [];
$.each(json, function(key, value){
    arr.push({
        type: key,
        value: value
    });
});