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
Javascript Articles
Page 97 of 534
Sort an array to have specific items first in the array - JavaScript
When working with arrays of objects, you might need to sort them so that items with specific properties appear first. This is a common requirement in applications where you need to prioritize certain elements while maintaining the original order of others. Suppose we have an array of objects like this: const arr = [ {flag: true, other: 1}, {flag: true, other: 2}, {flag: false, other: 3}, {flag: true, other: 4}, {flag: true, other: 5}, ...
Read MoreMerge two objects in JavaScript ignoring undefined values
When merging two objects in JavaScript, you may want to ignore undefined values to avoid overwriting valid data. The spread operator alone doesn't handle this logic, so we need a custom approach. The Problem Consider these two objects where some properties have undefined values: const A = { activity: 'purchased', count: undefined, time: '09:05:33' }; const B = { activity: 'purchased', count: '51', time: undefined }; // Simple spread operator overwrites values const simpleSpread = { ...A, ...B }; console.log("Simple spread:", simpleSpread); Simple spread: { activity: 'purchased', count: '51', time: undefined } ...
Read MoreConvert array into array of subarrays - JavaScript
We are required to write a JavaScript function that takes in an array of literals and returns a new array that have elements of the original array chunked into subarrays of length exactly 2. Now if the length of original array is not exactly divisible by 2, then the last subarray should have only one element. For example, if the input array is: const arr = [1, 2, 3, 4, 5, 6, 7]; Then the output should be: const output = [[1, 2], [3, 4], [5, 6], [7]] Using Loop-based ...
Read MoreJavaScript Algorithm - Removing Negatives from the Array
Given an array X of multiple values (e.g. [-3, 5, 1, 3, 2, 10]), we need to write a function that removes any negative values from the array using only the pop() method without creating temporary arrays. The challenge is to modify the original array in-place, maintaining only positive numbers while using just the pop() method for removal. Algorithm Approach The algorithm works in two phases: Remove all negative values from the end of the array For remaining negatives, replace them with the last positive element and pop Complete Implementation function ...
Read MoreHow to join JavaScript array of string
We need to write a JavaScript function that joins an array of strings, replaces all whitespaces with dashes "-", and returns the formatted string. For example: If the array is − const arr = ["QA testing promotion ", " Twitter ", "Facebook ", "Test"]; Then the output should be − const output = "QA-testing-promotion-Twitter-Facebook-Test"; Method 1: Using Loop and String Concatenation This approach joins the array elements and processes each character to replace spaces with dashes: const arr = ["QA testing promotion ", " Twitter ", "Facebook ", ...
Read MoreRecursive string parsing into object - JavaScript
Recursive string parsing transforms an array of dot-separated strings into a nested JavaScript object structure. This technique is useful for converting flat configuration paths into hierarchical data structures. Problem Description Given an array of strings following the pattern x.y.x.y..., we need to create a nested object where the last segment becomes a value in an array, and preceding segments form the nested structure. For example, if the input array is: const arr = [ "country.UK.level.1", "country.UK.level.2", "country.US.level.1", "country.UK.level.3" ...
Read MoreHow to sort date array in JavaScript
Sorting arrays of dates is a common task in JavaScript. This tutorial shows how to sort an array containing date strings in ascending chronological order. Suppose we have an array that contains some dates like this: const arr = [ [ '02/13/2015', 0.096 ], [ '11/15/2013', 0.189 ], [ '05/15/2014', 0.11 ], [ '12/13/2013', 0.1285 ], [ '01/15/2013', 0.12 ], [ '01/15/2014', 0.11 ], [ '02/14/2014', 0.11 ], ...
Read MoreHow do I console.log JavaScript variables as it relates to DOM?
To display variables on console when working with the DOM, you can use console.log() to output element objects, their properties, or values. The document.getElementById() method retrieves DOM elements which can then be logged to examine their properties. Logging DOM Elements When you log a DOM element directly, the browser console displays the element object with all its properties: Console Log DOM Example Hello World ...
Read MoreAdd property to common items in array and array of objects - JavaScript?
To add properties to objects based on common items in arrays, use the map() method combined with Set for efficient lookups. This approach allows you to conditionally add properties to array objects based on whether their values exist in another array. Example Arrays Let's start with a simple array and an array of objects: const firstname = ['John', 'David', 'Bob']; const studentDetails = [ { firstname: 'Carol', marks: 78 }, ...
Read MoreHow to edit values of an object inside an array in a class - JavaScript?
In JavaScript, you can edit values of objects inside an array within a class by using the this keyword. This allows you to reference and modify properties of objects stored in class arrays. Understanding the Structure When working with objects inside arrays in classes, each object can contain its own methods that modify its properties. The key is understanding how this refers to the current object context. Example Here's how to create a class with an array of objects and modify their values: class Employee { constructor() { ...
Read More