There is an object:

sptn={"orders":[{"externalOrderId":19,"externalCustomerId":0,"status":"INITIALIZED","date":"2017-08-10 19:25:17","paymentMethod":"Credit Card","restoreUrl":"market.com","deliveryAddress":", , ","items":[{"externalItemId":"52","name":"\u0441\u043a\u0438\u0434\u043a\u04302","category":"Cameras","quantity":1,"cost":444,"url":"\/index.php?route=product\/product&product_id=52","imageUrl":"","description":"<p><br><\/p>"}]}]} 

You must add more keys to it; add like this:

 sptn.orders =[{"ddd":44,"costin":444}] //стирается все элементы 

How can I add it to get this structure?

 sptn={"orders":[{"externalOrderId":19,"externalCustomerId":0,"ddd":44,"costin":444,"status":"INITIALIZED","date":"2017-08-10 19:25:17","paymentMethod":"Credit Card","restoreUrl":"market.com","deliveryAddress":", , ","items":[{"externalItemId":"52","name":"\u0441\u043a\u0438\u0434\u043a\u04302","category":"Cameras","quantity":1,"cost":444,"url":"\/index.php?route=product\/product&product_id=52","imageUrl":"","description":"<p><br><\/p>"}]}]} 

    1 answer 1

    Use the Push , Luke!

     let obj = [1, 2]; console.info(obj); // 1, 2 obj.push(3); console.info(obj); // 1, 2, 3 


    If you need to change a specific key of the array and add properties to it, then:

     let sptn = { "orders": [{ "externalOrderId": 19, "externalCustomerId": 0, "ddd": 44, "costin": 444, "status": "INITIALIZED", "date": "2017-08-10 19:25:17", "paymentMethod": "Credit Card", "restoreUrl": "market.com", "deliveryAddress": ", , ", "items": [{ "externalItemId": "52", "name": "\u0441\u043a\u0438\u0434\u043a\u04302", "category": "Cameras", "quantity": 1, "cost": 444, "url": "\/index.php?route=product\/product&product_id=52", "imageUrl": "", "description": "<p><br><\/p>" }] }] }; sptn.orders[0].ddd = 44; sptn.orders[0].costin = 444; console.info(sptn); 

    • sptn.orders.push ('clock': 44) VM1474: 1 Uncaught SyntaxError: missing) after argument list - Anton
    • sptn.orders.push ({"clock": 44}) // get two objects - Anton
    • @Anton, updated. - user207618
    • Oh nishtya thanks it works, hmm, when I studied, I didn’t study before the campaign (I don’t know that there is let beyond var) - Anton