
- 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
Top n max value from array of object JavaScript
Let’s say, we have an array of objects like this −
const arr = [ {"id":0,"start":0,"duration":117,"slide":4,"view":0}, {"id":0,"start":0,"duration":12,"slide":1,"view":0}, {"id":0,"start":0,"duration":41,"slide":2,"view":0}, {"id":0,"start":0,"duration":29,"slide":3,"view":0}, {"id":0,"start":0,"duration":123,"slide":3,"view":0}, {"id":0,"start":0,"duration":417,"slide":2,"view":0}, {"id":0,"start":0,"duration":12,"slide":1,"view":0}, {"id":0,"start":0,"duration":67,"slide":2,"view":0} ];
We have to write a function that takes in this array and returns the top n element of the array in another array (top means the object that has the highest value for duration).
Therefore, let’s write the code for this problem −
Example
const arr = [ {"id":0,"start":0,"duration":117,"slide":4,"view":0}, {"id":0,"start":0,"duration":12,"slide":1,"view":0}, {"id":0,"start":0,"duration":41,"slide":2,"view":0}, {"id":0,"start":0,"duration":29,"slide":3,"view":0}, {"id":0,"start":0,"duration":123,"slide":3,"view":0}, {"id":0,"start":0,"duration":417,"slide":2,"view":0}, {"id":0,"start":0,"duration":12,"slide":1,"view":0}, {"id":0,"start":0,"duration":67,"slide":2,"view":0} ]; const topN = (arr, n) => { if(n > arr.length){ return false; } return arr .slice() .sort((a, b) => { return b.duration - a.duration }) .slice(0, n); }; console.log(topN(arr, 3)); console.log(topN(arr, 4)); console.log(topN(arr, 5));
Output
The output in the console will be −
[ { id: 0, start: 0, duration: 417, slide: 2, view: 0 }, { id: 0, start: 0, duration: 123, slide: 3, view: 0 }, { id: 0, start: 0, duration: 117, slide: 4, view: 0 } ] [ { id: 0, start: 0, duration: 417, slide: 2, view: 0 }, { id: 0, start: 0, duration: 123, slide: 3, view: 0 }, { id: 0, start: 0, duration: 117, slide: 4, view: 0 }, { id: 0, start: 0, duration: 67, slide: 2, view: 0 } ] [ { id: 0, start: 0, duration: 417, slide: 2, view: 0 }, { id: 0, start: 0, duration: 123, slide: 3, view: 0 }, { id: 0, start: 0, duration: 117, slide: 4, view: 0 }, { id: 0, start: 0, duration: 67, slide: 2, view: 0 }, { id: 0, start: 0, duration: 41, slide: 2, view: 0 } ]
- Related Articles
- Get the max n values from an array in JavaScript
- Get max value per key in a JavaScript array
- Find Max Slice Of Array | JavaScript
- Return Top two elements from array JavaScript
- Creating a JavaScript Object from Single Array and Defining the Key Value?
- Average of array excluding min max JavaScript
- Retrieve user id from array of object - JavaScript
- Create array from JSON object JavaScript
- From JSON object to an array in JavaScript
- Building a frequency object from an array JavaScript
- How to modify an array value of a JSON object in JavaScript?
- map() array of object titles into a new array based on other property value JavaScript
- Sorting an array object by property having falsy value - JavaScript
- Extract key value from a nested object in JavaScript?
- Returning the highest number from object properties value – JavaScript

Advertisements