R
Reactiflux

help-js

✅ – ekomS – 23-35 Dec 21

OROwen Rossi-Keen12/21/2022
Howdy all. I've got this sort function which prioritizes truthy values in an array:
truthyFirst: (array, callback = emptyCallback) => {
return array.sort((a, b) => {
if (callback(a) && callback(b)) return 0;
if (callback(b)) return 1;
if (callback(a)) return -1;
});
},
truthyFirst: (array, callback = emptyCallback) => {
return array.sort((a, b) => {
if (callback(a) && callback(b)) return 0;
if (callback(b)) return 1;
if (callback(a)) return -1;
});
},
Is there a better way to coerce the condition to its relevant value? e.g. a && b -> 0, a -> -1, b -> 1 Got it Changed Lol, caught in the act Fair enough Thank you Another question, is there a better way to handle the emptyCallback bit? Currently I've got:
const emptyCallback = (...args) => args;
const emptyCallback = (...args) => args;
The idea being that if a callback is not provided to the function, it will return the parameters Are my arguments coerced into a boolean? Ah okay Well The value will always be a boolean
arrayFunctions.sort.truthyFirst(products, (product) => {
return product.metadata.status == 'In Stock';
})
arrayFunctions.sort.truthyFirst(products, (product) => {
return product.metadata.status == 'In Stock';
})
This is how I want to use the function Makes sense Perfect That's what I'm looking for Thank you I implemented both
UUUnknown User12/22/2022
3 Messages Not Public
Sign In & Join Server To View

Looking for more? Join the community!