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
Object Oriented Programming Articles
Page 390 of 588
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 ...
Read MoreCount unique elements in array without sorting JavaScript
Let’s say, we have an array of literals that contains some duplicate values −const arr = ['Cat', 'Dog', 'Cat', 'Elephant', 'Dog', 'Grapes', 'Dog', 'Lion', 'Grapes', 'Lion'];We are required to write a function that returns the count of unique elements in the array. Will use Array.prototype.reduce() and Array.prototype.lastIndexOf() to do this −Exampleconst arr = ['Cat', 'Dog', 'Cat', 'Elephant', 'Dog', 'Grapes', 'Dog', 'Lion', 'Grapes', 'Lion']; const countUnique = arr => { return arr.reduce((acc, val, ind, array) => { if(array.lastIndexOf(val) === ind){ return ++acc; }; return acc; }, 0); }; console.log(countUnique(arr));OutputThe output in the console will be −5
Read MoreReorder array based on condition in JavaScript?
Let’s say we have an array of object that contains the scores of some players in a card game −const scorecard = [{ name: "Zahir", score: 23 }, { name: "Kabir", score: 13 }, { name: "Kunal", score: 29 }, { name: "Arnav", score: 42 }, { name: "Harman", score: 19 }, { name: "Rohit", score: 41 }, { name: "Rajan", score: ...
Read MoreHow to create an ordered array from values that have an order number in JavaScript?
Let’s say, we have a series of strings, which contain and image path and order #concatenated. They look like this −const images = [ 'photo1.jpg, 0', 'photo2.jpg, 2', 'photo3.jpg, 1' ];Therefore, the correct order should be − photo1, photo3, photo2. What we need to do is process this into a correctly ordered array with just the path values. So, ultimately we need −const orderedImages = [ 'photo1.jpg', 'photo3.jpg', 'photo2.jpg' ]Let’s write the code to sort this array of images according to its correct order −Exampleconst images = [ 'photo1.jpg, 0', 'photo2.jpg, 2', ...
Read MoreAggregate records in JavaScript
Let’s say, we have an array of objects that contains information about some random transactions carried out by some people −const transactions = [{ name: 'Rakesh', amount: 1500 }, { name: 'Rajesh', amount: 1200 }, { name: 'Ramesh', amount: 1750 }, { name: 'Rakesh', amount: 2100 }, { name: 'Mukesh', amount: 1100 }, { name: 'Rajesh', amount: 1950 }, { name: 'Mukesh', ...
Read MoreConvert an array of binary numbers to corresponding integer in JavaScript
Let’s say, we have an array of Numbers that contains 0 and 1 −const arr = [0, 1, 0, 1];We are required to write an Array function, toBinary() that returns the corresponding binary for the array it is used with.For example − If the array is −const arr = [1, 0, 1, 1];Then the output should be 11 because the decimal representation of binary 1011 is 11.Therefore, let’s write the code for this function.Method 1: Using library methodsIn JavaScript, there exists a method parseInt(), that takes in two arguments first one is a string and second a number that represents ...
Read MoreReturn the maximum number in each array using map JavaScript
We have an array of arrays of Numbers like this one −const arr = [ [12, 56, 34, 88], [65, 66, 32, 98], [43, 87, 65, 43], [32, 98, 76, 83], [65, 89, 32, 54], ];We are required to write a function that maps over this array of arrays and returns an array that contains the maximum (greatest) element from each subarray.Therefore, let’s write the code for this function −Exampleconst arr = [ [12, 56, 34, 88], [65, 66, 32, 98], [43, 87, 65, 43], [32, 98, 76, 83], [65, 89, 32, 54], ]; const findMax = arr => { return arr.map(sub => { const max = Math.max(...sub); return max; }); }; console.log(findMax(arr));OutputThe output in the console will be −[ 88, 98, 87, 98, 89 ]
Read MoreExtract properties from an object in JavaScript
We have to write a JavaScript function, say extract() that extracts properties from an object to another object and then deletes them from the original object.For example −If obj1 and obj2 are two objects, thenobj1 = {color:"red", age:"23", name:"cindy"} obj2 = extract(obj1, ["color", "name"])After passing through the extract function, they should become like −obj1 = { age:23 } obj2 = {color:"red", name:"cindy"}Therefore, let’s write the code for this function −Exampleconst obj = { name: "Rahul", job: "Software Engineer", age: 23, city: "Mumbai", hobby: "Reading books" }; const extract = (obj, ...keys) => { const ...
Read MoreFinding matches in two elements JavaScript
We are required to write a function that returns true if the string in the first element of the array contains all of the letters of the string in the second element of the array.For example, ["hello", "Hello"], should return true because all of the letters in the second string are present in the first, ignoring their case.The arguments ["hello", "hey"] should return false because the string "hello" does not contain a "y".Lastly, ["Alien", "line"], should return true because all of the letters in "line" are present in "Alien".This is a fairly simple problem; we will just split the second ...
Read MoreFormatting dynamic json array JavaScript
Let’s say, we have an array of objects like this −const arr = [ {"name1": "firstString"}, {"name2": "secondString"}, {"name3": "thirdString"}, {"name4": "fourthString"}, {"name5": "fifthString"}, {"name6": "sixthString"}, ];We are required to write a function that takes one such array of objects and returns an object with all the properties listed in that object.So, let’s write the code for this function. It can be done through the Array reduce method −Exampleconst arr = [ {"name1": "firstString"}, {"name2": "secondString"}, {"name3": "thirdString"}, {"name4": "fourthString"}, {"name5": "fifthString"}, {"name6": "sixthString"}, ]; const reduceArray = ...
Read More