Object Oriented Programming Articles

Page 130 of 589

Is it possible to display substring from object entries in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 336 Views

Yes, you can display substrings from object entries in JavaScript using Object.fromEntries() combined with string methods like substr() or substring(). This technique allows you to transform object keys while preserving their associated values. Syntax Object.fromEntries( Object.entries(object).map(([key, value]) => [key.substr(startIndex, length), value] ) ) Example: Extracting Substring from Object Keys const originalString = { "John 21 2010": 1010, "John 24 2012": 1011, "John 22 2014": ...

Read More

Fix Problem with .sort() method in JavaScript, two arrays sort instead of only one

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 294 Views

One property of the Array.prototype.sort() function is that it is an in-place sorting algorithm, which means it does not create a new copy of the array to be sorted, it sorts the array without using any extra space, making it more efficient and performant. But this characteristic sometimes leads to an awkward situation. Let's understand this with an example. Assume, we have a names array with some string literals. We want to keep the order of this array intact and want another array containing the same elements as the names array but sorted alphabetically. We can do something ...

Read More

Remove same values from array containing multiple values JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 245 Views

In JavaScript, arrays often contain duplicate values that need to be removed. The most efficient modern approach is using Set with the spread operator to create a new array with unique values only. Example Array with Duplicates Let's start with an array containing duplicate student names: const listOfStudentName = ['John', 'Mike', 'John', 'Bob', 'Mike', 'Sam', 'Bob', 'John']; console.log("Original array:", listOfStudentName); Original array: [ 'John', 'Mike', 'John', 'Bob', 'Mike', 'Sam', 'Bob', 'John' ] Using Set with Spread Operator (Recommended) The Set object automatically removes duplicates, and the spread operator converts it ...

Read More

JavaScript : Why does % operator work on strings? - (Type Coercion)

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 444 Views

Let's say we have a code snippet that produces some surprising results. First, the modulo operator works with strings (unexpectedly). Second, concatenation of two strings produces awkward results. We need to explain why JavaScript behaves this way through type coercion. Problem Code const numStr = '127'; const result = numStr % 5; const firstName = 'Armaan'; const lastName = 'Malik'; const fullName = firstName + + lastName; console.log('modulo result:', result); console.log('full name:', fullName); modulo result: 2 full name: ArmaanNaN What is Type Coercion? Type coercion is JavaScript's automatic conversion of ...

Read More

Merge object properties through unique field then print data - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 209 Views

Let's say we have a students object containing two properties names and marks. The names is an array of objects with each object having two properties name and roll, similarly marks is an array of objects with each object having properties mark and roll. Our task is to combine the marks and names properties according to the appropriate roll property of each object. Problem Statement The students object is given here: const students = { marks: [{ roll: 123, ...

Read More

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 195 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

Why can't my HTML file find the JavaScript function from a sourced module?

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

When an HTML file cannot find JavaScript functions from a sourced module, it's usually because the function isn't properly exported from the module or the import statement is incorrect. ES6 modules require explicit export/import statements to share functions between files. The Export Problem Functions in JavaScript modules are not globally accessible unless explicitly exported. Without the export keyword, functions remain private to the module. demo.js console.log("function will import"); export function test(){ console.log("Imported!!!"); } Example: Complete Module Import index.html ...

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

How to remove an object using filter() in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 659 Views

The filter() method creates a new array with elements that pass a test condition. It's commonly used to remove objects from arrays based on specific criteria. Initial Array Let's start with an array of objects where some have a details property and others don't: let objectValue = [ { "id": "101", "details": { "Name": "John", "subjectName": "JavaScript" }}, { "id": "102", "details": { "Name": "David", "subjectName": "MongoDB" }}, { "id": "103" } ]; console.log("Original array:"); console.log(objectValue); Original array: [ ...

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
Showing 1291–1300 of 5,881 articles
« Prev 1 128 129 130 131 132 589 Next »
Advertisements