javascript“推送”无法识别,不确定原因

时间:2018-02-24 03:03:24

标签: javascript

我是javascript的新手。

我正在尝试向现有对象添加一个条目,作为在Raspberry Pi 3上开发Chromecast Sender应用程序的练习的一部分。它将基于具有变量结构的g00gle示例,如下所示。

我需要做的第一件事就是有信心操纵对象,添加,修改和删除项目。

在下面的示例中,“push”行引发错误,我不知道为什么。 任何帮助将不胜感激。

Uncaught TypeError: movies3.push is not a function at broken.html:28

<SCRIPT type="text/javascript">
//--------------------------------------------------------------------------------------------------
var movies3 = 
{ 'categories' : 
    [{ 'name' : 'Movies', 
                'videos' : [ { 'description' : "TEST 1",
                                   'sources' : ['http://TEST1.SOURCES.mp4'],
                                  'subtitle' : 'TEST1 SUBTITLE',
                                     'thumb' : 'TEST1 THUMB',
                                     'title' : 'TEST1 TITLE'
                             },
                             { 'description' : 'a2', 'sources' : ['a2.mp4'], 'subtitle' : 'a2 subtitle', 'thumb' : 'a2 thumb', 'title' : 'a2 title' }
                           ]
     }
    ]
};    
//--------------------------------------------------------------------------------------------------
movies3.push ( { categories: [ { name: 'Movies', videos: [ { description : "d1 description", sources : ['http://...d1...mp4'], subtitle : 'd1 subtitle', thumb : 'd1 thumb', title : 'd1 title' } ] } ] } );
//--------------------------------------------------------------------------------------------------
console.log(movies3);
document.write('<style>');
document.write('h1{text-align:left;}');
document.write('h4{text-align:left;margin:0;padding:0px;background:#f4f4f4;}');
document.write('li{text-align:left;margin:50;padding:0px}');
document.write('</style>');
document.write('<div id="container">');
document.write('<h1> My Favourite movies3</h1>');
for (var i = 0; i < movies3['categories'][0]['videos'].length; i++) {
   document.write('<h4>' + movies3['categories'][0]['videos'][i].title + '</h4>');
   document.write('<ul>');
   document.write('<li> subtitle    ' + movies3['categories'][0]['videos'][i].subtitle + '</li>');
   document.write('<li> description ' + movies3['categories'][0]['videos'][i].description + '</li>');
   document.write('<li> thumb       ' + movies3['categories'][0]['videos'][i].thumb + '</li>');
   for (var j = 0; j < movies3['categories'][0]['videos'][i].sources.length; j++) {
      document.write('<ul>');
      document.write('<li> sources ' + j + ' ' + movies3['categories'][0]['videos'][i].sources[j] + '</li>');
      document.write('</ul>');
   };
   document.write('</ul>');
};
document.write('</div>');
//--------------------------------------------------------------------------------------------------
</SCRIPT>

2 个答案:

答案 0 :(得分:1)

在您的代码中,您已将 movies 定义为对象。其中 categories array 。您不能在对象上使用.push,它只适用于数组。

你需要

var movies3 = 
{ 'categories' : 
    [{ 'name' : 'Movies', 
                'videos' : [ { 'description' : "TEST 1",
                                   'sources' : ['http://TEST1.SOURCES.mp4'],
                                  'subtitle' : 'TEST1 SUBTITLE',
                                     'thumb' : 'TEST1 THUMB',
                                     'title' : 'TEST1 TITLE'
                             },
                             { 'description' : 'a2', 'sources' : ['a2.mp4'], 'subtitle' : 'a2 subtitle', 'thumb' : 'a2 thumb', 'title' : 'a2 title' }
                           ]
     }
    ]
};    
//--------------------------------------------------------------------------------------------------
movies3.categories.push ({
  "name": "Movies",
  "videos": [
    {
      "description": "d1 description",
      "sources": [
        "http:p4"
      ],
      "subtitle": "d1 subtitle",
      "thumb": "d1 thumb",
      "title": "d1 title"
    }
  ]
});

console.log(movies3);

答案 1 :(得分:1)

您正在使用对象,数组可以访问.push原型。向对象添加新项目应如下所示

movies3.categories.push({ ... })

相关问题