
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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.
Therefore, let’s write the code for this function −
Example
The code for this will be −
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);
Output
The output in the console will be −
[ { key: 'a', value: 100 }, { key: 'a', value: 23 }, { key: 'a', value: false }, { key: 'a', value: null } ]
- Related Articles
- Pushing elements to a Stack in Javascript
- Pushing positives and negatives to separate arrays in JavaScript
- Pushing NaN to last of array using sort() in JavaScript
- How to create objects in JavaScript?
- Manipulating objects in array of objects in JavaScript
- Converting array of objects to an object of objects in JavaScript
- How to compare two objects in JavaScript?
- Identifying False values in JavaScript
- How to access nested json objects in JavaScript?
- How to shallow copy the objects in JavaScript?
- Convert object of objects to array in JavaScript
- Flat array of objects to tree in JavaScript
- Convert object to array of objects in JavaScript
- How to Sort/Order keys in JavaScript objects ?
- PreventDefault( ) vs Return false in JavaScript?

Advertisements