
- 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
Retrieving n smallest numbers from an array in their original order in JavaScript
Problem
We are required to write a JavaScript function that takes in an array of numbers arr, and a number n.
Our function should retrieve the n smallest from the array arr without disturbing their relative order. It means they should not be arranged in increasing or decreasing order rather they should hold their original order.
Example
Following is the code −
const arr = [6, 3, 4, 1, 2]; const num = 3; const smallestInOrder = (arr = [], num) => { if(arr.length < num){ return arr; }; const copy = arr.slice(); copy.sort((a, b) => a - b); const required = copy.splice(0, num); required.sort((a, b) => { return arr.indexOf(a) - arr.indexOf(b); }); return required; }; console.log(smallestInOrder(arr, num));
Output
Following is the console output −
[3, 1, 2]
- Related Articles
- Print n smallest elements from given array in their original order
- Smallest Common Multiple of an array of numbers in JavaScript
- Returning an array containing last n even numbers from input array in JavaScript
- Get the smallest array from an array of arrays in JavaScript
- Sorting numbers in ascending order and strings in alphabetical order in an array in JavaScript
- Smallest possible number divisible by all numbers from 1 to n in JavaScript
- JavaScript Program to Find k maximum elements of array in original order
- Retrieving object's entries in order with JavaScript?
- Rearrange An Array In Order – Smallest, Largest, 2nd Smallest, 2nd Largest,. Using C++
- Finding even length numbers from an array in JavaScript
- Third smallest number in an array using JavaScript
- Get n numbers from array starting from given point JavaScript
- Smallest number that is divisible by first n numbers in JavaScript
- Get the max n values from an array in JavaScript
- Find k maximum elements of array in original order in C++

Advertisements