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 130 of 589
Is it possible to display substring from object entries in JavaScript?
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 MoreFix Problem with .sort() method in JavaScript, two arrays sort instead of only one
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 MoreRemove same values from array containing multiple values JavaScript
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 MoreJavaScript : Why does % operator work on strings? - (Type Coercion)
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 MoreMerge object properties through unique field then print data - JavaScript
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 MoreAdding 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 MoreWhy can't my HTML file find the JavaScript function from a sourced module?
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 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 MoreHow to remove an object using filter() in JavaScript?
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 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 More