Javascript Array.slice()参数不是函数
我在通过freeCodeCamp测试版时遇到了一个奇怪的问题 这样做的“目的”不是修改原始数组,而是使用函数式编程技术修改数组 但是,我不断收到关于“array”参数的投诉,即remove函数不是有效函数:Javascript Array.slice()参数不是函数,javascript,Javascript,我在通过freeCodeCamp测试版时遇到了一个奇怪的问题 这样做的“目的”不是修改原始数组,而是使用函数式编程技术修改数组 但是,我不断收到关于“array”参数的投诉,即remove函数不是有效函数: // the global variable var bookList = [ "The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Natu
// the global variable
var bookList = [
"The Hound of the Baskervilles",
"On The Electrodynamics of Moving Bodies",
"Philosophiæ Naturalis Principia Mathematica",
"Disquisitiones Arithmeticae"];
/* This function should add a book to the list and return the list */
// New parameters should come before the bookName one
// Add your code below this line
function add (bookListTemp, bookName) {
let newBookArr = bookListTemp;
return newBookArr.push(bookName);
// Add your code above this line
}
/* This function should remove a book from the list and return the list */
// New parameters should come before the bookName one
// Add your code below this line
function remove (bookList,bookName) {
let newArr = bookList.slice();
if (newArr.indexOf(bookName) >= 0) {
return newArr.slice(0, 1, bookName);
// Add your code above this line
}
}
var newBookList = add(bookList, 'A Brief History of Time');
var newerBookList = remove(bookList, 'On The Electrodynamics of Moving Bodies');
var newestBookList = remove(add(bookList, 'A Brief History of Time'),
'On The Electrodynamics of Moving Bodies');
console.log(bookList);
在remove函数中,我尝试了获取参数并执行array.slice()方法以及array.concat()方法。因为执行让newArr=bookList
实际上并不能使新数组正确吗?它只是创建一个引用原始数组的新副本,对吗
我得到的确切错误是TypeError:bookList.slice不是函数
更奇怪的是Array。isArray(bookList)
返回true
(在函数remove
中)。所以我不明白它为什么抱怨数组方法?你的问题是
返回方法所基于的对象的新长度属性
打电话来
您应该返回数组
function add (bookListTemp, bookName) {
let newBookArr = bookListTemp;
newBookArr.push(bookName);
// Add your code above this line
return newBookArr;
}
或
让我们试试吧
你的问题是
返回方法所基于的对象的新长度属性
打电话来
您应该返回数组
function add (bookListTemp, bookName) {
let newBookArr = bookListTemp;
newBookArr.push(bookName);
// Add your code above this line
return newBookArr;
}
或
让我们试试吧
有两种方法可以在不改变数组的情况下复制数组。您将无法在bookList上使用
.slice()
方法,因为它是函数中的参数,因此不是数组。解决方法是var newBookArr=array.prototype.slice.call(bookListTemp);
或[].slice.call(bookListTemp);
这使您可以在bookList作为参数时对其执行切片。另一种方法是,在使用它时,我发现-var newBookArr=[].concat(bookListTemp);
当尝试
var newBookArr=[].push(bookListTemp);
时,我们发现原始图书列表被推到新数组中。因此它是一个副本,但作为数组中的一个数组。.concat()
方法将旧数组合并到新数组中。有两种方法可以在不改变数组的情况下复制数组。您将无法在bookList上使用.slice()
方法,因为它是函数中的参数,因此不是数组。解决方法是var newBookArr=array.prototype.slice.call(bookListTemp);
或[].slice.call(bookListTemp);
这使您可以在bookList作为参数时对其执行切片。另一种方法是,在使用它时,我发现-var newBookArr=[].concat(bookListTemp);
当尝试
var newBookArr=[].push(bookListTemp);
时,我们发现原始图书列表被推到新数组中。因此它是一个副本,但作为数组中的一个数组。.concat()
方法将旧数组合并到新数组中。您有两个调用remove
。哪一个调用会导致该错误?它说它发生在remove函数中。Jsfiddle指向let newArr=bookList.slice();
您的函数不会返回数组。[]push()
不返回数组,它返回结果长度。[].slice()
也不返回完整数组。您有两个调用remove
。哪个调用会给您带来该错误?它说它发生在remove函数中。Jsfiddle指向let newArr=bookList.slice();
您的函数没有返回数组。[].push()不返回数组,它返回结果长度。[].slice()
也不会返回完整的数组。不会将一个数组设置为另一个数组,只是对原始数组进行一个副本引用…因此,如果我最终更改了此副本,它不会弄乱原始数组。是的。因为concat会返回一个新的数组实例。因此,在我看来,您不需要添加函数。只需使用concat。MDN是一个很好的参考文档ence:)很遗憾,我无法摆脱挑战的add函数:/OK。因此,如果要推送,请返回newBookArr而不是newBookArr.push-请参阅我的编辑帖子:)最后通过确保将do.slice()插入每个数组的新数组来修复它。所以当我们通常复制一个数组时,比如让arrnew=oldarray。如果我们更改arrnew,它是否总是更改oldarray?将一个数组设置为另一个数组是否只是对原始数组进行一个副本引用…因此,如果我最终更改此副本,它不会弄乱原始数组。是的。因为concat返回一个新的数组实例。所以在我看来,你不需要功能添加。只是用海螺。MDN是很好的参考文件。:)不幸的是,我无法摆脱挑战的add函数:/OK。因此,如果要推送,请返回newBookArr而不是newBookArr.push-请参阅我的编辑帖子:)最后通过确保将do.slice()插入每个数组的新数组来修复它。所以当我们通常复制一个数组时,比如让arrnew=oldarray。如果我们更改arrnew,它是否总是更改oldarray?