Get Elements of an Array Based on Corresponding Values in JavaScript

AmitDiwan
Updated on 26-Aug-2020 11:41:41

599 Views

Suppose we have two arrays, for an example consider the following two −const array1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']; const array2 = [ 1, 0, 0, 1 , 0, 0, 1, 0];Both the arrays are bound to have the same length. We are required to write a function that when provided with an element from the second array, returns a subarray from the first array of the all elements whose index corresponds to the index of the element we took as an argument in the second array.For example: findSubArray(0) should return −[‘b’, ‘c’, ‘e’, ‘f’, ‘h’]Because ... Read More

Top N Max Value from Array of Objects in JavaScript

AmitDiwan
Updated on 26-Aug-2020 11:39:52

1K+ Views

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 More

Count Unique Elements in Array Without Sorting in JavaScript

AmitDiwan
Updated on 26-Aug-2020 11:38:09

362 Views

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

Reorder Array Based on Condition in JavaScript

AmitDiwan
Updated on 26-Aug-2020 11:35:08

2K+ Views

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 More

Create Ordered Array from Values with Order Number in JavaScript

AmitDiwan
Updated on 26-Aug-2020 11:30:42

166 Views

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 More

Merge Object and Sum a Single Property in JavaScript

AmitDiwan
Updated on 26-Aug-2020 11:28:30

690 Views

Let’s say, we have two arrays of objects that contain information about some products of a company −const first = [    {       id: "57e7a1cd6a3f3669dc03db58",       quantity:3    },    {       id: "57e77b06e0566d496b51fed5",       quantity:3    },    {       id: "57e7a1cd6a3f3669dc03db58",       quantity:3    },    {       id: "57e77b06e0566d496b51fed5",       quantity:3    } ]; const second = [    {       id: "57e7a1cd6a3f3669dc03db58",       quantity:6    },    {       id: "57e77b06e0566d496b51fed5",     ... Read More

Aggregate Records in JavaScript

AmitDiwan
Updated on 26-Aug-2020 11:26:46

2K+ Views

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 More

Convert Array of Binary Numbers to Corresponding Integer in JavaScript

AmitDiwan
Updated on 26-Aug-2020 11:24:28

6K+ Views

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 More

Return Maximum Number in Each Array Using Map in JavaScript

AmitDiwan
Updated on 26-Aug-2020 11:22:55

744 Views

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 ]

Find Average of Consecutive Elements in JavaScript

AmitDiwan
Updated on 26-Aug-2020 11:21:55

204 Views

Let’s say, we have an array of numbers −const arr = [3, 5, 7, 8, 3, 5, 7, 4, 2, 8, 4, 2, 1];We are required to write a function that returns an array with the average of the corresponding element and its predecessor. For the first element, as there are no predecessors, so that very element should be returned.Let’s write the code for this function, we will use the Array.prototype.map() function to solve this problem −Exampleconst consecutiveAverage = arr => {    return arr.map((el, ind, array) => {       return ((el + (array[ind-1] || 0)) / (1 + !!ind));    }); }; console.log(consecutiveAverage(arr));OutputThe output in the console will be −[    3, 4, 6, 7.5, 5.5, 4,    6, 5.5, 3, 5, 6, 3,    1.5 ]

Advertisements