Modern ES6 Array Filtering
TODO: collapasable, copyable, download, etc
Is there a more modern and idiomatic way to write return get(attributeDisplayKeyStore).filter( (groupBy) => selectedGroupBy.indexOf(groupBy) == -1 );
Yes, you can write this code in a more modern and idiomatic way using ES6 (ECMAScript 2015) features or later. The original code snippet you provided filters elements in an array returned by get(attributeDisplayKeyStore)
by checking if they are not included in selectedGroupBy
. A more modern way to write this could leverage the Array.includes()
method, which is more readable and idiomatic for checking if an array includes a certain element. Here's how you can rewrite it:
return get(attributeDisplayKeyStore).filter(
(groupBy) => !selectedGroupBy.includes(groupBy)
);
The Array.includes()
method makes the intent clearer and is more concise than using indexOf() == -1
. It directly checks if selectedGroupBy
contains groupBy
, and the use of the logical NOT operator (!
) negates the condition, so the filter includes elements not present in selectedGroupBy
. This approach is more modern and idiomatic for contemporary JavaScript development.
Related Chats
Modern ES6 Array Filtering 1.000
Using Sets for Uniqueness 0.438
New chat 0.433
Convert Objects to Set 0.426
Fix ExpandableGroupPanel Filter Error 0.424
Reactivity Troubleshooting in Svelte 0.401
Dynamic Auto-Complete Component 0.385
Series Stay Same 0.366
Check for Not Null 0.356
Find parent with class. 0.339