AmitDiwan has Published 10744 Articles

Sort nested array containing objects ascending and descending according to date in JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Nov-2020 10:14:41

2K+ Views

Suppose we have a JSON Object that contains a nested array like this −const arr = {    "DATA": [       {          "BookingID": "9513",          "DutyStart": "2016-02-11 12:00:00"       },       {          "BookingID": ... Read More

Converting array of objects to an object of objects in JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Nov-2020 10:12:22

3K+ Views

Suppose we have an array of objects like this −const arr = [{id:1, name:"aa"}, {id:2, name:"bb"}, {id:3, name:"cc"}];We are required to write a JavaScript function that takes in one such array and returns an object of the object where the key of each object should be the id property.Therefore, the ... Read More

Comparing array elements keeping count in mind in JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Nov-2020 10:10:54

156 Views

Suppose, we have two arrays of literals that contain the same number of elements. We are supposed to write a function that checks whether or not the both arrays contain the same elements appearing for the same number of times.If the arrays fulfil this condition, we return true, false otherwise.We ... Read More

Compare arrays using Array.prototype.every() in JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Nov-2020 10:09:25

177 Views

We are required to write a JavaScript function that takes in two arrays of literals. Then our function should return true if all the elements of first array are included in the second array, irrespective of their count, false otherwise.We have to use Array.prototype.every() method to make these comparisons.ExampleThe code ... Read More

Finding average in mixed data type array in JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Nov-2020 10:08:26

373 Views

Suppose, we have an array of mixed data types like this −const arr = [1, 2, 3, 4, 5, "4", "12", "2", 6, 7, "4", 3, "2"];We are required to write a JavaScript function that takes in one such array and returns the average of all such elements that are ... Read More

Sum JavaScript arrays repeated value

AmitDiwan

AmitDiwan

Updated on 20-Nov-2020 10:06:32

302 Views

Suppose, we have an array of objects like this −const arr = [ {'TR-01':1}, {'TR-02':3}, {'TR-01':3}, {'TR-02':5}];We are required to write a JavaScript function that takes in one such array and sums the value of all identical keys together.Therefore, the summed array should look like −const output = [ {'TR-01':4}, ... Read More

Sorting an array by date in JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Nov-2020 10:05:14

4K+ Views

Suppose, we have an array of objects like this −const arr = [{id: 1, date: 'Mar 12 2012 10:00:00 AM'}, {id: 2, date: 'Mar 8 2012 08:00:00 AM'}];We are required to write a JavaScript function that takes in one such array and sorts the array according to the date property ... Read More

Converting numbers to Indian currency using JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Nov-2020 10:03:50

3K+ Views

Suppose we have any number and are required to write a JavaScript function that takes in a number and returns its Indian currency equivalent.toCurrency(1000) --> ₹4, 000.00 toCurrency(129943) --> ₹1, 49, 419.00 toCurrency(76768798) --> ₹9, 23, 41, 894.00ExampleThe code for this will be −const num1 = 1000; const num2 = ... Read More

Calculating the LCM of multiple numbers in JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Nov-2020 10:02:27

998 Views

We are required to write a JavaScript function that takes in an array of numbers of any length and returns their LCM.We will approach this problem in parts −Part 1 − We will create a helper function to calculate the Greatest Common Divisor (GCD) of two numbersPart 2 − Then ... Read More

How to form a two-dimensional array with given width (columns) and height (rows) in JavaScript ?

AmitDiwan

AmitDiwan

Updated on 20-Nov-2020 10:00:32

405 Views

We are required to write a JavaScript function that takes in three arguments −height --> no. of rows of the array width --> no. of columns of the array val --> initial value of each element of the arrayThen the function should return the new array formed based on these ... Read More

Advertisements