如何访问数组中子数组的最后一个元素的字段?

时间:2018-06-21 09:58:53

标签: javascript jquery arrays

我正在使用一个数组(将json对象转换为数组)

[
   {
      "_id":{
         "$oid":"5ab8d6faff24ae1204000862"
      },
      "allotment_details":[
         {
            "allotment_id":"468986c5-2155-01e9-74cb-3ad05c66800d",
            "hostel_id":{
               "$oid":"5ae69fb4d2ccda0e70005551"
            },
            "room_id":"ca62d5b5-8fac-62c4-9e62-7e7b9ce9a714",
            "food_preference":"Vegetarian",
            "from_date":{
               "$date":{
                  "$numberLong":"1527372000000"
               }
            },
            "to_date":{
               "$date":{
                  "$numberLong":"1528063200000"
               }
            },
            "approved_by":{
               "$oid":"5af2bda9bc1e370f9c0036d9"
            },
            "date_of_allotment":{
               "$date":{
                  "$numberLong":"1529487464000"
               }
            }
         },
         {
            "allotment_id":"c17043a4-7b5b-a132-8226-a4594b61658d",
            "hostel_id":{
               "$oid":"5ae69fb4d2ccda0e70005551"
            },
            "room_id":"ca62d5b5-8fac-62c4-9e62-7e7b9ce9a714",
            "food_preference":"Vegetarian",
            "from_date":{
               "$date":{
                  "$numberLong":"1527372000000"
               }
            },
            "to_date":{
               "$date":{
                  "$numberLong":"1530223200000"
               }
            },
            "approved_by":{
               "$oid":"5af2bda9bc1e370f9c0036d9"
            },
            "date_of_allotment":{
               "$date":{
                  "$numberLong":"1529571140000"
               }
            }
         }
      ]
   }
]

我正在尝试调整以下代码行

var allotmentDetailsArray = data[0]['allotment_details'];
if (typeof allotmentDetailsArray != 'undefined')
{  
    if(allotmentDetailsArray.length == 0)
    {
        isStudentValidForAllotment =true;            
    }
    else
    {
        //traverse
        var to_date_of_last_sub_array= data[0]["allotment_details"][lastIndex]["to_date"];
    }
}

我想在allotment_details子数组的最后一个元素中获取“ to_date”的值...请帮助!

3 个答案:

答案 0 :(得分:3)

要获取数组的最后一个元素,可以使用pop()slice(-1).pop()在检索之前克隆该数组,这样就不会删除该元素。您可以从那里按名称访问每个属性。试试这个:

var data = [{
  "allotment_details": [{
    "to_date": {
      "$date": {
        "$numberLong": "1528063200000"
      }
    }
  }, {
    "to_date": {
      "$date": {
        "$numberLong": "1530223200000"
      }
    }
  }]
}]

var date = data[0].allotment_details.slice(-1).pop().to_date.$date.$numberLong;
console.log(date);

请注意,我从您提供的对象中删除了不相关的属性,以使答案更加简洁。

答案 1 :(得分:1)

更改以下行

var to_date_of_last_sub_array= data[0]["allotment_details"][lastIndex]["to_date"];

对此:

var to_date_of_last_sub_array= data[0]["allotment_details"][(data[0]["allotment_details"].length - 1)]["to_date"];

MDN Array.length documentation

答案 2 :(得分:1)

您可以这样做:

const data = [{"_id":{"$oid":"5ab8d6faff24ae1204000862"},"allotment_details":[{"allotment_id":"468986c5-2155-01e9-74cb-3ad05c66800d","hostel_id":{"$oid":"5ae69fb4d2ccda0e70005551"},"room_id":"ca62d5b5-8fac-62c4-9e62-7e7b9ce9a714","food_preference":"Vegetarian","from_date":{"$date":{"$numberLong":"1527372000000"}},"to_date":{"$date":{"$numberLong":"1528063200000"}},"approved_by":{"$oid":"5af2bda9bc1e370f9c0036d9"},"date_of_allotment":{"$date":{"$numberLong":"1529487464000"}}},{"allotment_id":"c17043a4-7b5b-a132-8226-a4594b61658d","hostel_id":{"$oid":"5ae69fb4d2ccda0e70005551"},"room_id":"ca62d5b5-8fac-62c4-9e62-7e7b9ce9a714","food_preference":"Vegetarian","from_date":{"$date":{"$numberLong":"1527372000000"}},"to_date":{"$date":{"$numberLong":"1530223200000"}},"approved_by":{"$oid":"5af2bda9bc1e370f9c0036d9"},"date_of_allotment":{"$date":{"$numberLong":"1529571140000"}}}]}];
const toDate = data[0].allotment_details[data[0].allotment_details.length - 1].to_date;

console.log(toDate);