Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从单独对象中的数组中删除对象_Javascript_Arrays_Object_Prototype - Fatal编程技术网

Javascript 如何从单独对象中的数组中删除对象

Javascript 如何从单独对象中的数组中删除对象,javascript,arrays,object,prototype,Javascript,Arrays,Object,Prototype,我有一个我认为相当标准的测试/家庭作业问题,我不能完全完成 它包括创建库、书架和图书对象。它要求在图书馆中放置书架,在书架上放置书籍;它还需要有一种方法将一本书放在书架上,然后从书架上取下一本书 我已经完成了大部分工作,但两天来,我一直在用非自我的方法把头撞在墙上,试图从书架上取下一本书 这是我的密码: function Library() { this.shelves = []; //Logs all books in the library this.allBooks = fu

我有一个我认为相当标准的测试/家庭作业问题,我不能完全完成

它包括创建库、书架和图书对象。它要求在图书馆中放置书架,在书架上放置书籍;它还需要有一种方法将一本书放在书架上,然后从书架上取下一本书

我已经完成了大部分工作,但两天来,我一直在用非自我的方法把头撞在墙上,试图从书架上取下一本书

这是我的密码:

function Library() {
  this.shelves = [];

  //Logs all books in the library
  this.allBooks = function () {
    for (i = 0; i < library.shelves.length; i++) {
        console.log(JSON.stringify(library.shelves[i].books));
    }
  };


}

function Shelf(number) {
  this.number = number;
  this.books = [];

  //This method takes in the shelf number and logs the books on that shelf
  Library.prototype.booksOnShelf = function (shelfNum) {
    var x = shelfNum - 1;

    console.log(JSON.stringify(library.shelves[x].books));
  };
}

function Book(title) {
  this.title = title;

  //Adds a book to a specified shelf
  Library.prototype.enshelf = function (shelfNumber, bookTitle) {
    var y = shelfNumber - 1;
    library.shelves[y].books.push(new Book(bookTitle));
  };

  //this.unshelf = function (shelfNumber, bookTitle) {

  }

var library = new Library();

//Adds shelves to the library
library.shelves.push(new Shelf(1));
library.shelves.push(new Shelf(2));
library.shelves.push(new Shelf(3));

//Adds books to the library
library.shelves[0].books.push(new Book("A Storm of Swords"));
library.shelves[0].books.push(new Book("A Clash of Kings"));
library.shelves[0].books.push(new Book("A Game of Thrones"));
library.shelves[1].books.push(new Book("A Feast for Crows"));
library.shelves[2].books.push(new Book("A Dream of Dragons"));
library.shelves[2].books.push(new Book("Winds of Winter"));
library.shelves[2].books.push(new Book("A Dream of Spring"));




//library.allBooks(); //logs all the books in the library
//library.booksOnShelf(3); // logs all the books on a specified shelf
//console.log(library.shelves.length);//logs # of shelves in library
//library.enshelf(2, "Harry Potter");
//library.booksOnShelf(2);
library.allBooks();
//.unshelf("A Storm of Swords"); //Haven't figured this out yet
//library.allBooks();
函数库(){
this.sheels=[];
//记录图书馆里的所有书
this.allBooks=函数(){
对于(i=0;i
到目前为止,除了unself方法外,其他方法都能正常工作,我已经尝试了许多不同的方法

如果有人能让我走上正确的道路,我将不胜感激。我一直在考虑将该方法放在另一个对象中。此外,我不使用任何“return”,这可能会有所帮助?当我尝试调用library.sheels[arg].books.splice(arg,arg)时,控制台告诉我无法执行此操作


总而言之,请帮我从书架上取下一本特定的书。

您以错误的方式混合了许多概念。我建议您从头开始,阅读更多关于“原型继承”的内容。你在构造函数中定义了一个原型,忘记声明
i
,你在
构造函数中使用
作为全局函数……很多事情都出错了。(OT)我在你的评论中还发现你试图使用错误的属性
//library.enshelf(2,“哈利波特”)
enshelf而不是
un
要检索一本书,你是在研究如何检索一本特定的书吗?告诉我这将帮助我构建一个可爱的loopRoko,这是一种将一本书放在书架上并起作用的方法。Brendan,我正在寻找从书架上取下一本特定的书,而不再有它。Elclars…这是一种至少我担心。我可能会从头开始。谢谢大家。