Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Web Development Articles
Page 481 of 801
Finding content of arrays on the basis of specific property in JavaScript?
When working with arrays of objects in JavaScript, you often need to find and merge content based on matching properties. The find() method combined with map() provides an effective solution for this common task. Understanding the Problem Consider two arrays of student records where you want to find matching students based on their roll numbers and merge or replace data from the second array when matches are found. Using map() with find() The map() method creates a new array by transforming each element, while find() searches for the first matching element in another array. ...
Read MoreCheck whether a string ends with some other string - JavaScript
In JavaScript, there are multiple ways to check if a string ends with another string. The most straightforward approach is using the built-in endsWith() method, though you can also implement custom solutions. Using String.endsWith() (Recommended) The endsWith() method is the standard way to check if a string ends with a specific substring: const str1 = 'this is just an example'; const str2 = 'ample'; console.log(str1.endsWith(str2)); // true console.log(str1.endsWith('temple')); // false console.log(str1.endsWith('example')); // true true false true Custom Implementation ...
Read MoreHow to convert an object into an array in JavaScript?
In JavaScript, there are several methods to convert an object into an array. The approach depends on whether you want the keys, values, or both from the object. Using Object.keys() to Get Keys Array The Object.keys() method returns an array of an object's property names: const student = { name: "Chris", age: 25, marks: 85, city: "New York" }; const keysArray = Object.keys(student); console.log(keysArray); [ 'name', 'age', 'marks', 'city' ] Using Object.values() to Get ...
Read MoreDeleting the duplicate strings based on the ending characters - JavaScript
We are required to write a JavaScript function that takes in an array of strings and deletes each one of the two strings that ends with the same character. For example, if the actual array is: const arr = ['Radar', 'Cat', 'Dog', 'Car', 'Hat']; Then we have to delete duplicates and keep only one string ending with the same character. In this case, 'Cat', 'Car', and 'Hat' all end with 't', 'r', and 't' respectively, so we need to remove duplicates based on the last character. How It Works The algorithm uses a ...
Read MoreHow to find all subsets of a set in JavaScript?
To find all subsets of a set in JavaScript, you can use the reduce() method along with map() to generate all possible combinations. A subset is any combination of elements from the original set, including the empty set and the set itself. How It Works The algorithm starts with an empty subset [[]] and for each element in the original array, it creates new subsets by adding that element to all existing subsets. Example const findAllSubsetsOfGivenSet = originalArrayValue => originalArrayValue.reduce( (givenSet, setValue) ...
Read MoreSwap certain element from end and start of array - JavaScript
We need to write a JavaScript function that accepts an array of numbers and a position k, then swaps the kth element from the beginning with the kth element from the end of the array. Understanding the Problem For an array with indices 0 to n-1, the kth element from start is at index k-1, and the kth element from end is at index n-k. We swap these two elements. Example Implementation const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; const swapNth = (arr, k) => { ...
Read MoreParse JSON in JavaScript to display a specific name/value pair?
To parse JSON in JavaScript, you can use the native JSON.parse() method or jQuery's parseJSON() function. To display specific name/value pairs, you can use array methods like forEach() or jQuery's $.each() function. Using Native JSON.parse() The modern approach uses the built-in JSON.parse() method: Parse JSON Example const APIData = '[{"Name":"John", "Age":21}, {"Name":"David", "Age":24}, {"Name":"Bob", ...
Read MoreShift certain array elements to front of array - JavaScript
We are required to write a JavaScript function that takes in an array of numbers. The function should bring all the 3-digit integers to the front of the array. Let's say the following is our array of numbers: const numList = [1, 324, 34, 3434, 304, 2929, 23, 444]; Understanding 3-Digit Numbers A 3-digit number is any integer between 100 and 999 (inclusive). We can check this using a simple condition: const isThreeDigit = num => num > 99 && num < 1000; console.log(isThreeDigit(324)); // true console.log(isThreeDigit(34)); // ...
Read MoreHow do I display only the visible text with jQuery?
To display only the visible text in jQuery, use the :visible selector. This selector targets elements that are currently visible on the page (not hidden with display:none, visibility:hidden, or other hiding methods). Syntax $(selector).filter(':visible') $(selector + ':visible') $(':visible') Example Visible Text with jQuery Hidden Text Visible Text ...
Read MoreSquare every digit of a number - JavaScript
We are required to write a JavaScript function that takes in a number and returns a new number in which all the digits of the original number are squared and concatenated. For example: If the number is − 9119 Then the output should be − 811181 because 9² is 81 and 1² is 1. Example Following is the code − const num = 9119; const squared = num => { const numStr = String(num); let res = ''; ...
Read More