AmitDiwan has Published 10744 Articles

Get the smallest array from an array of arrays in JavaScript

AmitDiwan

AmitDiwan

Updated on 24-Nov-2020 10:46:46

500 Views

Suppose, we have a nested array of arrays like this −const arr = [    ["LEFT", "RIGHT", "RIGHT", "BOTTOM", "TOP"],    ["RIGHT", "LEFT", "TOP"],    ["TOP", "LEFT"] ];We are required to write a JavaScript function that takes in one such array. The function then should pick the smallest subarray (smallest ... Read More

Most efficient method to groupby on an array of objects - JavaScript

AmitDiwan

AmitDiwan

Updated on 24-Nov-2020 10:45:05

263 Views

Suppose, we have an array of objects like this −const arr = [    { Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" },    { Phase: "Phase 1", Step: "Step 1", Task: "Task 2", Value: "10" },    { Phase: "Phase 1", Step: "Step 2", Task: ... Read More

Efficient algorithm for grouping elements and counting duplicates in JavaScript

AmitDiwan

AmitDiwan

Updated on 24-Nov-2020 10:40:42

348 Views

We have got an array of objects. If one property of the object is the same as in another object, we consider it to be a duplicate entry.We want to group objects by this property and store information about how many times the "duplicate" occurred.   X A B O   ... Read More

Find all disjointed intersections in a set of vertical line segments in JavaScript

AmitDiwan

AmitDiwan

Updated on 24-Nov-2020 10:38:22

152 Views

We have a set of vertical regions defined by y1 and y2 coordinates, where y1 is the starting point and y2 is the ending point of each region.The origin of our coordinates system is the top-left corner, so y2 is always greater than y1.This is an example −const regions = ... Read More

Data manipulation with JavaScript

AmitDiwan

AmitDiwan

Updated on 24-Nov-2020 10:36:38

2K+ Views

Suppose we have two arrays describing some cashflow like these −const months = ["jan", "feb", "mar", "apr"]; const cashflows = [    {'month':'jan', 'value':10},    {'month':'mar', 'value':20} ];We are required to write a JavaScript function that takes in two such arrays. Our function should then construct a combined array of ... Read More

Create an object based on 2 others in JavaScript

AmitDiwan

AmitDiwan

Updated on 24-Nov-2020 10:33:32

115 Views

Suppose, we have two JavaScript objects defined like this −const a = {    a: 1,    af: function() { console.log(this.a) }, }; const b = {    b: 2,    bf: function() { console.log(this.b) }, };We are required to write a JavaScript function that takes in two such objects. ... Read More

How can I remove a specific item from an array JavaScript?

AmitDiwan

AmitDiwan

Updated on 24-Nov-2020 10:32:13

279 Views

Let’s say, we have an array of numbers and we added elements to it. You need to devise a simple way to remove a specific element from an array.The following is what we are looking for −array.remove(number);We have to use core JavaScript. Frameworks are not allowed.ExampleThe code for this will ... Read More

How to get a timestamp in JavaScript?

AmitDiwan

AmitDiwan

Updated on 24-Nov-2020 10:30:39

305 Views

We are required to depict the ways in which we can access the current timestamp in JavaScript in −---seconds---millisecondsJavaScript works with the number of milliseconds since the epoch whereas most other languages work with the seconds.This will give you a Unix timestamp (in seconds) −const date = new Date(); const ... Read More

Return the greatest possible product of n numbers from the array in JavaScript

AmitDiwan

AmitDiwan

Updated on 24-Nov-2020 10:29:14

113 Views

We are required to write a JavaScript function that takes in an array of Numbers as the first argument and a number, say n, as the second argument.Our function should calculate and return the greatest possible product of n numbers from the array.ExampleThe code for this will be −const getHighestProduct ... Read More

Nested collection filter with JavaScript

AmitDiwan

AmitDiwan

Updated on 24-Nov-2020 10:28:17

1K+ Views

Suppose, we have an array of nested objects like this −const arr = [{    id: 1,    legs:[{       carrierName:'Pegasus'    }] }, {    id: 2,    legs:[{       carrierName: 'SunExpress'    },    {       carrierName: 'SunExpress'    }] }, { ... Read More

Advertisements