Length of Multiple Arrays

I know this line does what it's supposed to do:

Data.AppointmentList[0].AppointmentList[0].OrderNo + ", ";

I have to export all the OrderNo there exist in all AppointmentList.AppointmentList

From this array (Not the correct one):

{
    "AppointmentList": [
        {
            "AppointmentList": [
                {
                    "OrderNo": 111,
                    "OrderNo": 222
                }
            ]
        }
}

JS:

$.each(Data, function(i, item){
  for(i= 0; i < Object.keys(Data.AppointmentList.AppointmentList.OrderNo).length; i++) {
      document.getElementById("divlist").innerHTML += Data.AppointmentList[0].AppointmentList[0].OrderNo + ", ";
    }
})

Need this output:

111, 222

Upvotes: 1

Views: 1377

Answers (1)

junvar
junvar

Reputation: 11604

let input = {
  "AppointmentList": [
    {"AppointmentList": [{"OrderNo": 1}, {"OrderNo": 2}]},
    {"AppointmentList": [{"OrderNo": 3}]},
    {"AppointmentList": [{"OrderNo": 4}, {"OrderNo": 5}, {"OrderNo": 6}]}
  ]
};

let orders = input.AppointmentList.flatMap(a => a.AppointmentList).map(a => a.OrderNo);

console.log(orders.join(', '));

Upvotes: 6

Related Questions