Javascript 如果不是,我如何消除所有这些
我编写此代码是为了过滤“books”数组,具体取决于作为参数给定的作者或流派:-Javascript 如果不是,我如何消除所有这些,javascript,filter,graphql,Javascript,Filter,Graphql,我编写此代码是为了过滤“books”数组,具体取决于作为参数给定的作者或流派:- //This code works perfectly fine as is allBooks: (root, args) => { if (args.author === undefined && args.genre === undefined) { return books } else if (args.author === undefined)
//This code works perfectly fine as is
allBooks: (root, args) => {
if (args.author === undefined && args.genre === undefined) {
return books
} else if (args.author === undefined) {
return books.filter((book) => book.genres.includes(args.genre))
} else if (args.genre === undefined) {
return books.filter((book) => book.author === args.author)
} else {
const booksFilter = books.filter((book) => book.author === args.author)
return booksFilter.filter((book) => book.genres.includes(args.genre))
}
}
我相信一定有某种方法可以更专业地写这篇文章,而不用使用所有这些。如果有人知道更好的方法,我会很感激的
[已编辑]
多亏了大家,我决定使用ghostkraviz解决方案,代码现在看起来如下:
allBooks: (root, args) => {
return books.filter((book) => {
const filteredGenres = book.genres.filter((genre) =>
genre.includes(args.genre || "")
);
return book.author.includes(args.author || "") && filteredGenres.length > 0;
})
}
我不知道下面的版本是否写得更“专业”,因为它看起来很凌乱,但它是一行,不使用if-else
books = [{
author: "a",
genres: ["a"]},
{author: "a",
genres: ["a", "b"]},
{author: "b",
genres: ["c", "b"]
}];
allBooks = (root, args) => {
return (!args.author && !args.genre) ? books : (!args.author) ?
books.filter((book) => book.genres.includes(args.genre)):(!args.genre)?
books.filter((book) => book.author === args.author) :
(books.filter((book) => book.author === args.author)).filter((book) =>
book.genres.includes(args.genre));
}
console.log(allBooks("",{genre: "b"}));
//console:
//0: {author: "a", genres: Array(2)}
//1: {author: "b", genres: Array(2)}
因为String.prototype.includes实际上检查一个字符串是否在另一个字符串(参数)中找到。这意味着,对于未定义的参数,可以将其默认为空字符串。如果选中任何字符串中的.includes,则空字符串将返回true 您只检查2个参数,它们是作者和类型。 下面是一个例子:
const book=[
{作者:“A”,体裁:[“恐怖”、“浪漫”]},
{作者:“B”,体裁:[“浪漫”]},
{作者:“X”,体裁:[“科学”]},
{作者:“C”,体裁:[“科学”,“恐怖”]}
];
const allBooks=(根,参数)=>{
返回图书。过滤器(图书=>{
const filteredGenres=book.genres.filter(genre=>
流派。包括(args.genre | | |“”)
);
return book.author.includes(args.author | |“”)和&filteredGenres.length>0;
});
};
log('filteredhorror=',allBooks({},{genre:'Horror'}));
log('filtereda and Horror=',allBooks({},{author:'A',genre:'Horror'}));
log('filteredroman=',allBooks({},{genre:'roman'}));
//所有的书都有结果
log('filteredroman=',allBooks({},{}));
//为了一个作者的结果
log('filteredauthor(A)=',allBooks({},{author:'A}))代码>您可以使用一个数组来使用键/值对进行过滤,如
filters = [
['author', 'eliot'],
['genre', 'fiction']
]
以及一个用于存储特殊类型搜索的对象,如
methods = {
genre: 'includes'
}
一起,您可以得到以下函数
result = books.filter(book => filter.every(([key, value]) => key in methods
? book[key][methods[key]](value)
: book[key] === value
));
由于空数组的返回值为true
,因此无需进一步操作即可获取所有书籍。我喜欢使用if-else,因为它更具可读性。我们还可以消除else if,因为每个if语句都有一个返回
这只是现有代码的简化版本,没有else-if和花括号
allBooks: (root, args) => {
const { author, genre } = args;
if (!author && !genre) return books;
if (!author) return books.filter((book) => book.genres.includes(genre))
if (!genre) return books.filter((book) => book.author === author);
return books.filter((book) => book.author === author).filter((book) => book.genres.includes(genre))
}
有时候,这与专业地编写代码无关。
这是关于编写易于理解的代码。您可以使用switch。在这个问题上,书籍有多种体裁,而不仅仅是一种。哦,该死,是的,填充修复这个hi,这部分起作用,如果我只使用author进行筛选,它将返回一个空数组。