Javascript Articles

Page 97 of 534

Sort an array to have specific items first in the array - JavaScript

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

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 More

Merge two objects in JavaScript ignoring undefined values

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

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 More

Convert array into array of subarrays - JavaScript

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

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 More

JavaScript Algorithm - Removing Negatives from the Array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 351 Views

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 More

How to join JavaScript array of string

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 185 Views

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 More

Recursive string parsing into object - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 589 Views

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 More

How to sort date array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 502 Views

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 More

How do I console.log JavaScript variables as it relates to DOM?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 164 Views

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 More

Add property to common items in array and array of objects - JavaScript?

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

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 More

How to edit values of an object inside an array in a class - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 304 Views

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
Showing 961–970 of 5,340 articles
« Prev 1 95 96 97 98 99 534 Next »
Advertisements