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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 193 Views

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 More

How to combine two arrays into an array of objects in JavaScript?

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

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 More

How to find the one integer that appears an odd number of times in a JavaScript array?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 270 Views

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 More

Can JavaScript parent and child classes have a method with the same name?

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

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 More

JavaScript array.includes inside nested array returning false where as searched name is in array

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

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 More

Remove values in an array by comparing the items 0th index in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 309 Views

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 More

Finding out the Harshad number JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 664 Views

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 More

Convert buffer to readable string in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 789 Views

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 More

Create a polyfill to replace nth occurrence of a string JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 583 Views

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 More

JavaScript outsider function call and return the result

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 227 Views

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
Showing 4571–4580 of 8,010 articles
« Prev 1 456 457 458 459 460 801 Next »
Advertisements