Javascript Articles

Page 241 of 534

How to implement merge sort in JavaScript?

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 23-Sep-2022 9K+ Views

Merge Sort The Merge Sort algorithm, where we can sort the elements in a particular order. This algorithm is also considered as an example of divide and conquer strategy. In merge sort algorithm, firstly the array will be divided into two parts and combined a particular sorted manner. The array will be divided into half until it cannot be divided. This means that if the array is completely divided and cannot be further divided, the dividing will be stopped. We divide the arrays into halves and implement merge sort on each of the halves. This algorithm is a ...

Read More

How to Split large string in to n-size chunks in JavaScript?

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 23-Sep-2022 3K+ Views

A string is an order of one or more characters that contain numbers, letters, symbols, or special characters. In JavaScript strings are immutable. i.e., once you create a string you cannot change its value. For example, consider the following snippet here we have created a string variable and assigned a value (Tutorialspoint) to it. In the next statement, we are trying to change the contents of the string at the first index. Then we are displaying the contents of the string. let x = 'Tutorialspoint'; x[0] = 't'; console.log(x); //Tutorialspoint. On executing this code ...

Read More

Sort array of objects by string property value in JavaScript

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 22-Sep-2022 3K+ Views

The given task is to sort an array of objects by using string property value in JavaScript. Assume there is an array of objects and we need to sort those elements by using the string property. In this below scenario, we are sorting array of objects by "company" property. const array = [ {Company: 'Oneplus', Manufacturing: 'China'}, {Company: 'Samsung', Manufacturing: 'South korea'}, {Company: 'Nothing', Manufacturing: 'India'} ]; Output = // it sorted the array of objects by "company" property. [ {"Company":"Nothing", "Manufacturing":"India"}, {"Company":"Oneplus", "Manufacturing":"China"}, ...

Read More

How to replace elements in array with elements of another array in JavaScript?

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 22-Sep-2022 7K+ Views

The given task is to replace the elements in an array with elements of another array. Input-Output Scenario Let’s look into some input-output scenarios. Consider there are there two arrays with elements in it. The first array is having more elements than the second array and we are replacing a part of elements in the first array with the elements from second array. Array1 = [1, 3, 5, 7, 2, 4]; Array2 = [3, 6]; Output = [3, 6, 5, 7, 2, 4] // elements got replaced Let’s consider another scenario, where the elements in the first array are ...

Read More

Best way to find length of JSON object in JavaScript

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 22-Sep-2022 17K+ Views

In this article, the given task is to get the best way of finding the length of a JSON object in JavaScript. Input-Output Scenario Let’s look into this input-output scenario. Consider there is an object with some keys and values in it. we need to get the length of the object. const MyObj = { Name: "Mike", Age: 34 }; document.write(Object.keys(MyObj).length); // Output: 2 Using Object.keys() The Object.keys() method will return the output in form of array of a given object’s own enumerable properties names. It will return the output of keys ...

Read More

Removing Negatives from Array in JavaScript

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 22-Sep-2022 2K+ Views

The given task is to remove the negative values from the array. Input-Output Scenario Let’s look into input output scenarios. Consider there is an array and it is having both negative and positive integer values. We need to remove the negative values from the array and return the array. Input = [-2, 5, -7, 32, 78, -32]; Output = [5, 32, 78] Now let’s assume there are no negatives integer values in the array. so, the array will be returned in the same order. Input = [56, 43, 12, 67, 69, 34]; Output = [56, 43, 12, 67, 69, ...

Read More

Join arrays to form string in JavaScript

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 22-Sep-2022 446 Views

The given task is to perform joining arrays to form strings in JavaScript. Input-Output Scenario Let’s look into some input-output scenarios. Consider there is an array having some elements in it and we are trying to join that array to form the string. Input = [32, 45, 65, 12, 07, 55]; Output = 32, 45, 65, 12, 07, 55 //String Let’s look into another scenario, where we are having two arrays and we are trying to join those two arrays and form a string. Array1 = [123, 453, 656, 654, 125, 757]; Array2 = ["Hello", "honey", "bunny"]; Output = ...

Read More

Match specific word in regex in JavaScript?

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 22-Sep-2022 11K+ Views

The task is to match a specific word or character which is in regex with a string. The regex (regular expressions) is a pattern that is used to match character combinations in strings. Here we include the test(), match(), and matchAll() methods to match the following word in regex. We have some boundary-type assertions, in which we have used \b. Consider a sentence – “mickey is holding mic.” Using regex - \bmic\b will match the word mic but not the word mic in mickey. It is a word boundary. Another assertion is (g), It is a global search flag. Consider ...

Read More

Reorder an array in JavaScript

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 22-Sep-2022 10K+ Views

The given task is to reorder an array in JavaScript. We can reorder the elements in the array by using the following methods. One of the ways to achieve the above task is b using sort() method. The sort() is an in-built method in JavaScript, which sorts the alphabetic elements. By default, it sorts in ascending order. Example Following is the example where the array got reordered in ascending order − const States = ["Telangana", "Uttar Pradesh", "Karnataka", "Kerala", ...

Read More

Return an array of all the indices of minimum elements in the array in JavaScript

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 22-Sep-2022 1K+ Views

An Array in JavaScript is a single variable where it can store different elements. These elements are stored at contiguous memory locations. Array elements can be accessed with the help of index numbers. The index numbers will start from 0. Syntax Below is the basic declaration of the array in JavaScript − Cosnt cars = [Maruti, Hyundai, Honda]; We need to return an array of all the indices of minimum elements in a JavaScript array Let’s look into the input-output scenario Assume there is an integer array where the minimum element is repeated more than once. We need to ...

Read More
Showing 2401–2410 of 5,338 articles
« Prev 1 239 240 241 242 243 534 Next »
Advertisements