简体   繁体   中英

How to push an object into a nested array

First I declare my array as

var Ulturls = [];

and if try to push an object

 var p ={'url' :'https://someurl.com', 'name':'someName', 'id': 'adast'}
 Ulturls.data.push(p)

I need to use push since I want my array to looks like this

Ulturls = [
    'data': [
               {
                 "name": "Usain Bolt is the real bro_video.mp4",
                 "url": "https://v.redd.it/l1yuug6bcc551/DASH_480",
                 "id": "cln8vskbjlbozk"
               },
               {
                 "name": "Usain Bolt is the real bro_audio.mp3",
                 "url": "https://v.redd.it/l1yuug6bcc551/audio",
                 "id": "cln8vskbjlbozl"
               }
          ]
     'total': 2
 ]

This isn't valid syntax:

Ulturls = [
    'data': [...]
    'total': 2
 ]

What I think you're looking for is an object, not an array. Try this:

var Ulturls = {
    data: []
}
Ulturls.data.push({
    "name": "Usain Bolt is the real bro_video.mp4",
    "url": "https://v.redd.it/l1yuug6bcc551/DASH_480",
    "id": "cln8vskbjlbozk"
});
Ulturls.data.push({
    "name": "Usain Bolt is the real bro_audio.mp3",
    "url": "https://v.redd.it/l1yuug6bcc551/audio",
    "id": "cln8vskbjlbozl"
});
Ulturls.total = Ulturls.data.length;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM