Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Pushing false objects to bottom in JavaScript
Suppose we have an array of objects like this ?
const array = [
{key: 'a', value: false},
{key: 'a', value: 100},
{key: 'a', value: null},
{key: 'a', value: 23}
];
We are required to write a JavaScript function that takes in one such array and places all the objects that have falsy values for the "value" property to the bottom and sorts all other objects in decreasing order by the "value" property.
Understanding Falsy Values
In JavaScript, falsy values include: false, null, undefined, 0, "" (empty string), and NaN. However, we need to be careful with the number 0, as it might be a valid value in some contexts.
Solution
We'll create a helper function to identify falsy values and then use a custom sorting function:
const arr = [
{key: 'a', value: false},
{key: 'a', value: 100},
{key: 'a', value: null},
{key: 'a', value: 23}
];
const isValFalsy = (obj) => !obj.value && typeof obj.value !== 'number';
const sortFalsy = arr => {
arr.sort((a, b) => {
if(isValFalsy(a) && isValFalsy(b)){
return 0;
}
if(isValFalsy(a)){
return 1;
};
if(isValFalsy(b)){
return -1;
};
return b.value - a.value;
});
};
sortFalsy(arr);
console.log(arr);
[
{ key: 'a', value: 100 },
{ key: 'a', value: 23 },
{ key: 'a', value: false },
{ key: 'a', value: null }
]
How It Works
The isValFalsy function checks if a value is falsy but excludes numbers (including 0) since they might be valid values. The sorting function works as follows:
- If both objects have falsy values, they maintain their relative order (return 0)
- If only the first object has a falsy value, it goes to the bottom (return 1)
- If only the second object has a falsy value, it goes to the bottom (return -1)
- If both have truthy values, sort in descending order by value
Alternative Approach with Separate Arrays
Another approach is to separate falsy and truthy objects, sort them separately, and combine:
const arr2 = [
{key: 'b', value: 0},
{key: 'b', value: 50},
{key: 'b', value: undefined},
{key: 'b', value: 75}
];
const sortFalsyAlternative = (array) => {
const truthyObjects = array.filter(obj => obj.value || typeof obj.value === 'number');
const falsyObjects = array.filter(obj => !obj.value && typeof obj.value !== 'number');
truthyObjects.sort((a, b) => b.value - a.value);
return [...truthyObjects, ...falsyObjects];
};
const result = sortFalsyAlternative(arr2);
console.log(result);
[
{ key: 'b', value: 75 },
{ key: 'b', value: 50 },
{ key: 'b', value: 0 },
{ key: 'b', value: undefined }
]
Conclusion
Both approaches effectively separate falsy objects from truthy ones and sort the truthy objects in descending order. The first method modifies the original array, while the second creates a new sorted array, giving you flexibility based on your needs.
