
- 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
Shift certain array elements to front of array - JavaScript
We are required to write a JavaScript function takes in an array of numbers. The function should bring all the 3-digit integers to the front of the array.
Let’s say the following is our array of numbers −
const numList = [1, 324,34, 3434, 304, 2929, 23, 444];
Example
Following is the code −
const numList = [1, 324,34, 3434, 304, 2929, 23, 444]; const isThreeDigit = num => num > 99 && num < 1000; const bringToFront = arr => { for(let i = 0; i < arr.length; i++){ if(!isThreeDigit(arr[i])){ continue; }; arr.unshift(arr.splice(i, 1)[0]); }; }; bringToFront(numList); console.log(numList);
Output
This will produce the following output in console −
[ 444, 304, 324, 1, 34, 3434, 2929, 23 ]
- Related Articles
- Shift last given number of elements to front of array JavaScript
- Shifting certain elements to the end of array JavaScript
- JavaScript Array shift()
- Array shift() in JavaScript
- Reversing array without changing the position of certain elements JavaScript
- How to remove certain number elements from an array in JavaScript
- Java Program to shift array elements to the right
- Java Program to shift array elements to the left
- Shift the bits of array elements of a Two-Dimensional array to the left in Numpy
- Shift the bits of array elements of a Two-Dimensional array to the right in Numpy
- Shift the bits of integer array elements to the left in Numpy
- Shift the bits of integer array elements to the right in Numpy
- Write the importance of shift() method in javascript array?
- How to replace elements in array with elements of another array in JavaScript?
- Accumulating array elements to form new array in JavaScript

Advertisements