试图缩短冗余Javascript代码
这是一个正常工作的javascript代码。然而,在我看来这是多余的。有什么办法可以清理一下吗试图缩短冗余Javascript代码,javascript,ecmascript-6,Javascript,Ecmascript 6,这是一个正常工作的javascript代码。然而,在我看来这是多余的。有什么办法可以清理一下吗 let text = 'Some search text'; const searchMatch = entry.title.toLowerCase().includes(text.toLowerCase()) || entry.description.toLowerCase().includes(text.toLowerCase()) || entry.keywords.toLowerCas
let text = 'Some search text';
const searchMatch =
entry.title.toLowerCase().includes(text.toLowerCase()) ||
entry.description.toLowerCase().includes(text.toLowerCase()) ||
entry.keywords.toLowerCase().includes(text.toLowerCase());
return searchMatch;
您可以使用数组和
。一些测试替代:
const textLower = text.toLowerCase();
return ['title', 'description', 'keywords']
.map(prop => entry[prop].toLowerCase())
.some(s => s.includes(textLower));
如果碰巧,entry
仅包含这些属性,则可以使用Object.values
:
return Object.values(entry)
.map(s => s.toLowerCase())
.some(s => s.includes(textLower));
您可以使用数组和。一些测试替代:
const textLower = text.toLowerCase();
return ['title', 'description', 'keywords']
.map(prop => entry[prop].toLowerCase())
.some(s => s.includes(textLower));
如果碰巧,entry
仅包含这些属性,则可以使用Object.values
:
return Object.values(entry)
.map(s => s.toLowerCase())
.some(s => s.includes(textLower));
你可以这样做:
const text = 'Some search text'.toLowerCase();
return [entry.title, entry.description, entry.keywords].some(s => s.toLowerCase().includes(text));
你可以这样做:
const text = 'Some search text'.toLowerCase();
return [entry.title, entry.description, entry.keywords].some(s => s.toLowerCase().includes(text));
您可以只使用一行返回语句,该语句涉及由entry.description
、entry.keywords
和entry.title
组成的数组,然后使用返回布尔值(true
/false
),具体取决于是否有任何测试通过:
return [entry.description, entry.keywords, entry.title].some(string => string.toLowerCase().includes('Some search text'.toLowerCase());
以下是每个部分的基本分类:
[entry.description, entry.keywords, entry.title].some(...)
这将生成一个匿名数组,由entry.description
、entry.keywords
和entry.title
(顺序不重要)组成,并使用array.prototype.some()方法对其进行迭代。根据,.some()
:
some()
方法测试数组中是否至少有一个元素通过了所提供函数实现的测试
基本上遍历每个元素,具体取决于所提供函数的回调,并提供一个布尔值(true
如果数组中至少有一个元素通过测试,如果没有元素通过测试,false
)
这是.some()
方法中包含的匿名函数,它接受单个参数字符串
。然后它返回一个布尔值,具体取决于.includes()
方法的结果。.includes()
方法返回另一个布尔值,具体取决于小写的字符串是否包含小写的“某些搜索文本”
。这是一口,但简而言之,上面的代码行是:
如果小写形式的字符串
包含小写形式的'Some search text'
,则返回true
——否则返回false
希望这对你有帮助 您可以只使用一行返回语句,该语句涉及由entry.description
、entry.keywords
和entry.title
)组成的数组,然后使用返回布尔值(true
/false
),具体取决于是否有任何测试通过:
return [entry.description, entry.keywords, entry.title].some(string => string.toLowerCase().includes('Some search text'.toLowerCase());
以下是每个部分的基本分类:
[entry.description, entry.keywords, entry.title].some(...)
这将生成一个匿名数组,由entry.description
、entry.keywords
和entry.title
(顺序不重要)组成,并使用array.prototype.some()方法对其进行迭代。根据,.some()
:
some()
方法测试数组中是否至少有一个元素通过了所提供函数实现的测试
基本上遍历每个元素,具体取决于所提供函数的回调,并提供一个布尔值(true
如果数组中至少有一个元素通过测试,如果没有元素通过测试,false
)
这是.some()
方法中包含的匿名函数,它接受单个参数字符串
。然后它返回一个布尔值,具体取决于.includes()
方法的结果。.includes()
方法返回另一个布尔值,具体取决于小写的字符串是否包含小写的“某些搜索文本”
。这是一口,但简而言之,上面的代码行是:
如果小写形式的字符串
包含小写形式的'Some search text'
,则返回true
——否则返回false
希望这对你有帮助 你可以做的一件事text=text.toLowerCase()
你可以做的一件事text=text.toLowerCase()