How To Include The Entire Length Of Arrays?
Below I created this filter method to only display what's found within the fullDetails array. I apologize if this question is unclear and if I'm using the wrong terminology. Right
Solution 1:
If your filter is supposed to keep items whose fullDetails
array contains this.state.searchItems
(case-insensitive), you're looking for the some
method of arrays:
let lc = this.state.searchItems.toLowerCase(); // Do this oncelet modItemList = this.props.items.filter(
(item) => item.fullDetails.some(e => e.toLowerCase().indexOf(lc) !== -1)
);
some
calls its callback for each entry in the array, stopping and returning true
if the callback returns a truthy value (if it never does, some
returns false
).
Instead of indexOf
, you could also use includes
(ES2015, like let
and arrow functions):
let lc = this.state.searchItems.toLowerCase(); // Do this oncelet modItemList = this.props.items.filter(
(item) => item.fullDetails.some(e => e.toLowerCase().includes(lc))
);
Solution 2:
You can use every
to check if every element matches.
let modItemList = this.props.items.filter(
(item) => {
return item.fullDetails.every(fullDetail => fullDetail.toLowerCase().indexOf(this.state.searchItems.toLowerCase()) !== -1);
}
);
However, the plural in searchItems
indicates that this could be an array either - so maybe you need to flip/enhance the logic here.
Post a Comment for "How To Include The Entire Length Of Arrays?"