无法获得高阶函数找到工作
Can't get higher order function find to work
我是一个相当新的 webdev 学生,我目前正在做一个我们得到的练习,但我被卡住了。我有一个带有 objects 的数组,目标是使用 higher-order 函数来操作它们。
let bookList = [
{
title:"The Way of Kings",
author: "B Sanderson",
pages: 900,
isAvailable:false
},
{
title:"Words of radiance",
author: "B Sanderson",
pages: 1087,
isAvailable:true
},
{
title:"Oathbringer",
author: "B Sanderson",
pages: 1000,
isAvailable:false
}
];
我得到了一些代码作为起点,我不能更改。如果我的 bookList 中存在确切的标题,我应该编写一个 returns true 的函数。
function hasBook(title, bookShelf) {
}
这就是我目前的情况,我不知道如何进一步推进。在这里我得到一个错误,说 bookList 不是一个函数,但我不知道如何让它工作。我知道我搞砸了一些事情,我可能不完全理解如何使用 find 和默认代码。
function hasBook(title, bookShelf) {
if (title === bookShelf.titel) {
return true;
}
}
bookList.find(hasBook("Oathbringer", bookList ));
希望您明白我的意思。
您可以使用 some
方法 -
some()
方法测试数组中是否至少有一个元素通过提供的函数实现的测试。它 returns 一个布尔值。
let bookList = [
{
title:"The Way of Kings",
author: "B Sanderson",
pages: 900,
isAvailable:false
},
{
title:"Words of radiance",
author: "B Sanderson",
pages: 1087,
isAvailable:true
},
{
title:"Oathbringer",
author: "B Sanderson",
pages: 1000,
isAvailable:false
}
];
function hasBook(title, bookShelf) {
return bookShelf.some((o) => o.title.toLowerCase() === title.toLowerCase());
}
console.log(hasBook('Oathbringer', bookList));
你可以这样做:
function hasBook(title, bookShelf) {
const book = bookShelf.find(book => book.title === title)
return book ? true : false;
}
那么,你可以这样称呼它:
const result = hasBook("Oathbringer", bookList)
下面是一个完整的工作示例:
let bookList = [
{
title:"The Way of Kings",
author: "B Sanderson",
pages: 900,
isAvailable:false
},
{
title:"Words of radiance",
author: "B Sanderson",
pages: 1087,
isAvailable:true
},
{
title:"Oathbringer",
author: "B Sanderson",
pages: 1000,
isAvailable:false
}
];
function hasBook(title, bookShelf) {
const book = bookShelf.find(book => book.title === title)
return book ? true : false;
}
console.log(`Has book ${hasBook("Oathbringer", bookList)}`);
我是一个相当新的 webdev 学生,我目前正在做一个我们得到的练习,但我被卡住了。我有一个带有 objects 的数组,目标是使用 higher-order 函数来操作它们。
let bookList = [
{
title:"The Way of Kings",
author: "B Sanderson",
pages: 900,
isAvailable:false
},
{
title:"Words of radiance",
author: "B Sanderson",
pages: 1087,
isAvailable:true
},
{
title:"Oathbringer",
author: "B Sanderson",
pages: 1000,
isAvailable:false
}
];
我得到了一些代码作为起点,我不能更改。如果我的 bookList 中存在确切的标题,我应该编写一个 returns true 的函数。
function hasBook(title, bookShelf) {
}
这就是我目前的情况,我不知道如何进一步推进。在这里我得到一个错误,说 bookList 不是一个函数,但我不知道如何让它工作。我知道我搞砸了一些事情,我可能不完全理解如何使用 find 和默认代码。
function hasBook(title, bookShelf) {
if (title === bookShelf.titel) {
return true;
}
}
bookList.find(hasBook("Oathbringer", bookList ));
希望您明白我的意思。
您可以使用 some
方法 -
some()
方法测试数组中是否至少有一个元素通过提供的函数实现的测试。它 returns 一个布尔值。
let bookList = [
{
title:"The Way of Kings",
author: "B Sanderson",
pages: 900,
isAvailable:false
},
{
title:"Words of radiance",
author: "B Sanderson",
pages: 1087,
isAvailable:true
},
{
title:"Oathbringer",
author: "B Sanderson",
pages: 1000,
isAvailable:false
}
];
function hasBook(title, bookShelf) {
return bookShelf.some((o) => o.title.toLowerCase() === title.toLowerCase());
}
console.log(hasBook('Oathbringer', bookList));
你可以这样做:
function hasBook(title, bookShelf) {
const book = bookShelf.find(book => book.title === title)
return book ? true : false;
}
那么,你可以这样称呼它:
const result = hasBook("Oathbringer", bookList)
下面是一个完整的工作示例:
let bookList = [
{
title:"The Way of Kings",
author: "B Sanderson",
pages: 900,
isAvailable:false
},
{
title:"Words of radiance",
author: "B Sanderson",
pages: 1087,
isAvailable:true
},
{
title:"Oathbringer",
author: "B Sanderson",
pages: 1000,
isAvailable:false
}
];
function hasBook(title, bookShelf) {
const book = bookShelf.find(book => book.title === title)
return book ? true : false;
}
console.log(`Has book ${hasBook("Oathbringer", bookList)}`);