AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 412 of 840

Formatting dynamic json array JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 889 Views

Let's say, we have an array of objects like this – const arr = [ {"name1": "firstString"}, {"name2": "secondString"}, {"name3": "thirdString"}, {"name4": "fourthString"}, {"name5": "fifthString"}, {"name6": "sixthString"}, ]; We are required to write a function that takes one such array of objects and returns an object with all the properties listed in that object. So, let's write the code for this function. It can be done through the Array reduce method – Using Array.reduce() Method ...

Read More

JavaScript Return an array that contains all the strings appearing in all the subarrays

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 342 Views

We have an array of arrays like this − const arr = [ ['foo', 'bar', 'hey', 'oi'], ['foo', 'bar', 'hey'], ['foo', 'bar', 'anything'], ['bar', 'anything'] ] We are required to write a JavaScript function that takes in such array and returns an array that contains all the strings which appears in all the subarrays. Let's write the code for this function Example const arr = [ ['foo', 'bar', 'hey', 'oi'], ['foo', 'bar', 'hey'], ...

Read More

How to get sequence number in loops with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

To get sequence numbers in loops with JavaScript, you can use various approaches. The most common method is maintaining a counter variable that increments with each iteration. Using forEach() with External Counter The forEach() method provides an elegant way to iterate through arrays while maintaining sequence numbers using an external counter: let studentDetails = [ { id: 101, details: [{name: 'John'}, {name: 'David'}, {name: 'Bob'}] }, { ...

Read More

Array of multiples - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 988 Views

We are required to write a JavaScript function that takes in two numbers, say m and n, and it returns an array of first n multiples of m. For example − If the numbers are 4 and 6 Then the output should be − const output = [4, 8, 12, 16, 20, 24] Example Following is the code − const num1 = 4; const num2 = 6; const multiples = (num1, num2) => { const res = []; for(let i = num1; i ...

Read More

JavaScript - Redirect a URL

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 778 Views

In JavaScript, redirecting a user from one URL to another is a straightforward task. There are multiple ways to achieve this, but we'll focus on two of the most common and effective methods: window.location.href and window.location.replace(). Both are widely used for different purposes, and understanding when to use each one can help improve user navigation on your site. Approaches to Redirect a URL in JavaScript Following are the different approaches to redirect a URL: Using window.location.href Using window.location.replace() Redirect a URL Using window.location.href Property The ...

Read More

Formatted Strings Using Template Strings in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 335 Views

Template strings (template literals) in JavaScript allow you to create formatted strings with embedded expressions using backticks (`). They provide a cleaner alternative to string concatenation and offer multiline support. Syntax `string text ${expression} string text` Basic Example Template Strings Example Template String Formatting Show Formatted String ...

Read More

Finding matches in two elements JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 259 Views

We are required to write a function that returns true if the string in the first element of the array contains all of the letters of the string in the second element of the array. For example: ["hello", "Hello"], should return true because all of the letters in the second string are present in the first, ignoring their case. The arguments ["hello", "hey"] should return false because the string "hello" does not contain a "y". Lastly, ["Alien", "line"], should return true because all of the letters in "line" are present in "Alien". This ...

Read More

How to split last n digits of each value in the array with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 366 Views

We have an array of mixed values like numbers and strings, and we want to extract the last n digits from each element that has enough characters. const arr = ["", 20191219, 20191220, 20191221, 20191222, 20191223, 20191224, 20191225]; We need to write a JavaScript function that takes this array and a number n. If an element contains more than or equal to n characters, the function should return only the last n characters. Otherwise, the element should remain unchanged. Solution Here's how we can implement this function using the map() method and string manipulation: ...

Read More

How can I find the index of a 2d array of objects in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

To find the index of a specific object in a two-dimensional array, you need to search through both rows and columns. This involves using nested loops to traverse the matrix structure. Syntax function find2DIndex(matrix, searchCondition) { for (let row = 0; row < matrix.length; row++) { for (let col = 0; col < matrix[row].length; col++) { if (searchCondition(matrix[row][col])) { ...

Read More

Concatenating variable number of arrays into one - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 595 Views

We are required to write a JavaScript function that takes in any number of JavaScript arrays and returns one single array with all the values from input arrays concatenated into it. For example − If the input arrays are − [1, 5], [44, 67, 3], [2, 5], [7], [4], [3, 7], [6] Then the output should be − const output = [1, 5, 44, 67, 3, 2, 5, 7, 4, 3, 7, 6]; Using reduce() and concat() The most straightforward approach uses the reduce() method combined with concat() to merge ...

Read More
Showing 4111–4120 of 8,392 articles
« Prev 1 410 411 412 413 414 840 Next »
Advertisements