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 458 of 801
Adding two arrays of objects with existing and repeated members of two JavaScript arrays replacing the repeated ones
When working with arrays of objects in JavaScript, you often need to merge them while avoiding duplicates. This tutorial shows how to combine two arrays of objects and remove duplicates based on a specific property. The Problem Consider two arrays of objects where some objects have the same name property. We want to merge them into one array, keeping only unique entries based on the name field. const first = [{ name: 'Rahul', age: 23 }, { name: 'Ramesh', age: ...
Read MoreHow to combine two arrays into an array of objects in JavaScript?
Combining two arrays into an array of objects is a common task in JavaScript. This allows you to pair corresponding elements from both arrays into structured data. Using map() with Object Creation The map() method creates a new array by transforming each element. We can use it to combine arrays into objects: var firstArray = ['John', 'David', 'Bob']; var secondArray = ['Mike', 'Sam', 'Carol']; var arrayOfObjects = firstArray.map(function(value, index) { return { first: value, second: ...
Read MoreHow to find the one integer that appears an odd number of times in a JavaScript array?
We are given an array of integers and told that all the elements appear for an even number of times except a single element. Our job is to find that element in single iteration. Let this be the sample array: [1, 4, 3, 4, 2, 3, 2, 7, 8, 8, 9, 7, 9] Understanding XOR Operator Before attempting this problem, we need to understand a little about the bitwise XOR (^) operator. The XOR operator returns TRUE if both the operands are complementary to each other and returns FALSE if both the operands ...
Read MoreCan JavaScript parent and child classes have a method with the same name?
Yes, JavaScript parent and child classes can have methods with the same name. This concept is called method overriding, where the child class provides its own implementation of a method that already exists in the parent class. Method Overriding Example class Parent { constructor(parentValue) { this.parentValue = parentValue; } // Parent class method showValues() { console.log("The parent method is called....."); ...
Read MoreJavaScript array.includes inside nested array returning false where as searched name is in array
When working with nested arrays in JavaScript, the standard includes() method only checks the first level of the array. This article explores why this happens and provides a simple solution using JSON.stringify() to search through multidimensional arrays. The Problem The Array.prototype.includes() method only performs shallow comparison, meaning it cannot find elements nested within sub-arrays: const names = ['Ram', 'Shyam', ['Laxman', 'Jay']]; console.log(names.includes('Ram')); // true - found at first level console.log(names.includes('Laxman')); // false - nested inside sub-array true false Solution: Using JSON.stringify() A simple approach ...
Read MoreRemove values in an array by comparing the items 0th index in JavaScript?
When working with arrays of sub-arrays, you may need to remove duplicates based on the first element (0th index) of each sub-array. This is common when dealing with data like subject-marks pairs where you want only unique subjects. Let's say the following is our array: var subjectNameAlongWithMarks = [ ["JavaScript", 78], ["Java", 56], ["JavaScript", 58], ["MySQL", 77], ["MongoDB", 75], ["Java", 98] ]; console.log("Original array:", subjectNameAlongWithMarks); Original array: [ ...
Read MoreFinding out the Harshad number JavaScript
Harshad numbers are those numbers which are exactly divisible by the sum of their digits. Like the number 126, it is completely divisible by 1+2+6 = 9. All single digit numbers are harshad numbers. Harshad numbers often exist in consecutive clusters like [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [110, 111, 112], [1010, 1011, 1012]. Our job is to write a function that takes in ...
Read MoreConvert buffer to readable string in JavaScript?
In Node.js, buffers store binary data and need to be converted to strings for readability. The toString() method with encoding parameters handles this conversion. What is a Buffer? A Buffer is a Node.js object that represents a fixed-size chunk of memory allocated outside the JavaScript heap. It's used to handle binary data directly. Basic Buffer to String Conversion Use the toString() method with the appropriate encoding. UTF-8 is the most common encoding for text data. // Create a buffer from string var actualBufferObject = Buffer.from('[John Smith]', 'utf8'); console.log("The actual buffer object:"); console.log(JSON.stringify(actualBufferObject)); // ...
Read MoreCreate a polyfill to replace nth occurrence of a string JavaScript
A polyfill is a piece of code that provides functionality that isn't natively supported. In this tutorial, we'll create a polyfill to remove the nth occurrence of a substring from a string in JavaScript. Problem Statement We need to create a polyfill function removeStr() that extends the String prototype. The function should: subStr → the substring whose nth occurrence needs to be removed num → the occurrence number to remove (1st, 2nd, 3rd, etc.) Return the modified string if successful, or -1 if the nth occurrence doesn't exist ...
Read MoreJavaScript outsider function call and return the result
In JavaScript, you can call functions from outside their defining scope by using the return keyword to return inner functions. This creates closures that maintain access to outer variables. Syntax function outerFunction() { // Outer variables var outerVar = value; // Inner function var innerFunction = function() { // Can access outerVar return result; } ...
Read More