How to manage an array of objects with variable length?


#1

Hi everyone!
In code bellow length of array items is variable, it can be 1, 2, 3 … but now its 2.

          const order = {
            "order_type": "Sales",
            "parent": res['orders'][i].name,
            "customer": res['orders'][i].customer.first_name,
            "transaction_date": res['orders'][i].processed_at,
            "items": [
              {
                "item_code": res['orders'][i].line_items[0].id,
                "item_name": res['orders'][i].line_items[0].title,
                "delivery_date": res['orders'][i].processed_at,
                "qty": res['orders'][i].line_items[0].quantity,
                "rate": res['orders'][i].total_price
              },
              {
                "item_code": res['orders'][i].line_items[1].id,
                "item_name": res['orders'][i].line_items[1].title,
                "delivery_date": res['orders'][i].processed_at,
                "qty": res['orders'][i].line_items[1].quantity,
                "rate": res['orders'][i].total_price
              },
               ...
              
            ]
          }

how to loop?

for(let j=0;j<line_items.length;j++){
"items": [
              {
                "item_code": res['orders'][i].line_items[j].id,
                "item_name": res['orders'][i].line_items[j].title,
                "delivery_date": res['orders'][i].processed_at,
                "qty": res['orders'][i].line_items[j].quantity,
                "rate": res['orders'][i].total_price
              },
]
}