To simplify your code, you can utilize a concise one-line return statement that utilizes an array containing entry.description
, entry.keywords
, and entry.title
. By applying the Array.prototype.some()
method, you can determine a Boolean (true
/false
) value based on whether any of the specified tests are successful:
return [entry.description, entry.keywords, entry.title].some(string => string.toLowerCase().includes('Some search text'.toLowerCase());
Here's a brief breakdown of each component:
[entry.description, entry.keywords, entry.title].some(...)
This segment constructs an anonymous array consisting of entry.description
, entry.keywords
, and entry.title
(order is irrelevant) and performs iteration using the Array.prototype.some()
method. As stated on the MDN page, .some()
:
The some()
method evaluates if at least one element in the array meets the condition defined by the provided function.
It essentially loops through each element and generates a Boolean output (returns true
if at least one element matches the condition, and false
otherwise).
string => string.toLowerCase().includes('Some search text'.toLowerCase())
This represents the unnamed function within the .some()
operation, accepting a single parameter string
. It delivers a Boolean outcome predicated on the result of the .includes()
function, which ascertains whether the lowercase version of string
encompasses the lowercase variant of 'Some search text'
. In essence, the aforementioned line of code interprets as follows:
If the lowercased representation of string
includes the lowercased form of 'Some search text'
, then return true
; otherwise, yield false
.
Hopefully, this explanation assists you!