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
Object Oriented Programming Articles
Page 169 of 589
How to know if two arrays have the same values in JavaScript?
In JavaScript, comparing arrays directly with == or === compares references, not values. To check if two arrays contain the same values (regardless of order), we need custom comparison logic. The Problem with Direct Comparison var firstArray = [100, 200, 400]; var secondArray = [400, 100, 200]; console.log(firstArray === secondArray); // false console.log([1, 2] === [1, 2]); // false - different objects false false Using Sort and Compare Method The most reliable approach is to sort both arrays and compare ...
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 remove the first character of link (anchor text) in JavaScript?
In JavaScript, you can remove the first character from link anchor text using the substring(1) method combined with DOM manipulation. This is useful when you need to programmatically fix incorrectly formatted link text. The Problem Sometimes links may have extra characters at the beginning that need to be removed. For example, "Aabout_us" should be "about_us" and "Hhome_page" should be "home_page". Using substring(1) Method The substring(1) method extracts characters from index 1 to the end, effectively removing the first character (index 0). ...
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 MoreAppending a key value pair to an array of dictionary based on a condition in JavaScript?
In JavaScript, you can append key-value pairs to objects within an array or dictionary based on conditions using Object.assign() or the spread operator. This is useful when you need to add properties conditionally. Problem Statement Given a dictionary of student objects, we want to add a lastName property based on whether the student's name appears in a specific array. Using Object.assign() with Conditional Logic The following example demonstrates how to append a lastName property to each student object based on a condition: const details = { john: {'studentName': 'John'}, ...
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 MoreThe best way to remove duplicates from an array of objects in JavaScript?
When working with arrays of objects in JavaScript, removing duplicates requires checking specific properties rather than comparing entire objects. Here are the most effective approaches. Sample Data Let's use this array of student objects with duplicate IDs: var studentDetails = [ {studentId: 101}, {studentId: 104}, {studentId: 106}, {studentId: 104}, {studentId: 110}, {studentId: 106} ]; console.log("Original array:", studentDetails); Original array: [ { studentId: 101 }, ...
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 MoreHow can I find the index of a 2d array of objects in JavaScript?
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 MoreSorting Array with JavaScript reduce function - JavaScript
We are required to write a JavaScript function that takes in an array of numbers. The function should sort the array without using the Array.prototype.sort() method. We are required to use the Array.prototype.reduce() method to sort the array. Let's say the following is our array: const arr = [4, 56, 5, 3, 34, 37, 89, 57, 98]; Understanding the Approach The reduce() method can be used to build a sorted array by iterating through each element and placing it in the correct position within an accumulator array. We'll use insertion sort logic within the ...
Read More