简体   繁体   English

如何将 object 推入嵌套数组

[英]How to push an object into a nested array

First I declare my array as首先我将我的数组声明为

var Ulturls = [];

and if try to push an object如果尝试推送 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我需要使用 push 因为我希望我的数组看起来像这样

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.我认为您正在寻找的是 object,而不是数组。 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 object 推入嵌套在对象数组中的数组中? - How to push an object into an array nested in an array of objects? ZCCADCDEDB567ABAE643E15DCF0974E503Z - 如何将 object 推送到嵌套的对象数组中 - Mongoose - How to push object in nested array of objects React-如何将对象推入嵌套在数组中的数组 - React - How to Push an Object to an Array Nested within an Array 对象嵌套数组,动态推送 - Nested Array of object, push dynamically 如何动态构建一个javascript对象并在嵌套循环中推入数组? - How to dynamically build a javascript Object and push into Array within nested loop? 如何将参数推入多个嵌套的数组/对象结构中? - How do I push a parameter into a multiple nested array/object structure? 如何将数组推送到仅匹配父属性的嵌套子对象? - How to push array to nested child object of only matching parent properties? 如何将嵌套对象的项目推入数组(未定义项目) - How to push item of nested object into array (item not defined) 如何使用 React 和 TypeScript 在嵌套数组中推送 object? - How to push an object inside nested array using React and TypeScript? MongoDB 和 mongoose:如何将 object 推送到嵌套的文档数组? - MongoDB with mongoose: how to push an object to a nested array of documents?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM