AmitDiwan has Published 10744 Articles

Stop making form to reload a page in JavaScript

AmitDiwan

AmitDiwan

Updated on 18-Aug-2020 07:08:16

8K+ Views

Let’s say what we need to achieve is when the user submits this HTML form, we handle the submit event on client side and prevent the browser to reload as soon as the form is submittedHTML form Now, the easiest and the most reliable way of doing so ... Read More

Convert object of objects to array in JavaScript

AmitDiwan

AmitDiwan

Updated on 18-Aug-2020 07:06:34

3K+ Views

Let’s say we have the following object of objects that contains rating of some Indian players, we need to convert this into an array of objects with each object having two properties namely name and rating where name holds the player name and rating holds the rating object −Following is ... Read More

Is there any way I can call the validate() function outside the initValidation() function in JavaScript?

AmitDiwan

AmitDiwan

Updated on 18-Aug-2020 07:02:11

177 Views

We wish to call the function validate() outside of initValidation(), without necessarily having to call initValidation()Following is our problem code −function initValidation(){    // irrelevant code here    function validate(_block){       // code here    } }In JavaScript, as we know that functions are nothing but objects, so ... Read More

Sorting by 'next' and 'previous' properties (JS comparator function)

AmitDiwan

AmitDiwan

Updated on 18-Aug-2020 07:00:57

355 Views

Here is our sample array of object, consider each object as representing some page of a multipage website, each object have a next property (unless it represents the last page) that points to some id of another object and a previous property (unless it represents the first page) that points ... Read More

Sorting arrays by two criteria in JavaScript

AmitDiwan

AmitDiwan

Updated on 18-Aug-2020 06:58:19

3K+ Views

Let the following be the array to be sorted by date and isImportant. All the objects with isImportant property true rank higher than any any of the object with isImportant false and both the groups sorted according to the date property.Following is our array −const array = [{    id: ... Read More

How do I search through an array using a string, which is split into an array with JavaScript?

AmitDiwan

AmitDiwan

Updated on 18-Aug-2020 06:56:19

223 Views

We are given an array of strings and another string for which we are required to search in the array. We can filter the array checking whether it contains all the characters that user provided through the input.The code for doing the same would be −ExampleSolution 1const deliveries = ["14/02/2020, ... Read More

Is it possible to have JavaScript split() start at index 1?

AmitDiwan

AmitDiwan

Updated on 18-Aug-2020 06:55:16

355 Views

As of the official String.prototype.split() method there exist no way to start splitting a string from index 1 or for general from any index n, but with a little tweak in the way we use split(), we can achieve this functionality.We followed the following approach −We will create two arrays ... Read More

Flat a JavaScript array of objects into an object

AmitDiwan

AmitDiwan

Updated on 18-Aug-2020 06:54:08

2K+ Views

To flat a JavaScript array of objects into an object, we created a function that takes array of object as only argument. It returns a flattened object with key append by its index. The time complexity is O(mn) where n is the size of array and m is the number ... Read More

PHP program to find the maximum element in an array

AmitDiwan

AmitDiwan

Updated on 17-Aug-2020 12:21:27

2K+ Views

To find the maximum element in an array, the PHP code is as follows −Example Live DemoOutputThe highest value of the array is91A function named ‘get_max_value()’ is defined, that takes an array as parameter. Inside this function, the count function is used to find the number of elements in the array, ... Read More

PHP program to check if a given number is present in an infinite series or not

AmitDiwan

AmitDiwan

Updated on 17-Aug-2020 12:17:49

159 Views

To check if a given number is present in an infinite series or not, the PHP code is as follows −Example Live DemoOutputThe number is not present in the infinite seriesAbove, three variables are defined, and the function is called by passing these three values −$m = 3; $n = 5; ... Read More

Advertisements