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
Articles by AmitDiwan
Page 392 of 840
Remove elements from array using JavaScript filter - JavaScript
Suppose, we have two arrays of literals like these − const arr1 = [4, 23, 7, 6, 3, 6, 4, 3, 56, 4]; const arr2 = [4, 56, 23]; We are required to write a JavaScript function that takes in these two arrays and filters the first to contain only those elements that are not present in the second array. And then return the filtered array to get the below output − const output = [7, 6, 3, 6, 3]; Method 1: Using filter() with indexOf() The filter() method creates ...
Read MoreRemove leading zeros in a JavaScript array?
To remove leading zeros from a JavaScript array, we use the filter() method with a closure function that tracks when the first non-zero element is encountered. Once a non-zero value is found, all subsequent elements (including zeros) are kept. Input Examples [10, 0, 12, 0, 0] [0, 0, 0, 0, 0, 0, 10, 12, 0] [12, 0, 0, 1, 0, 0] Example const removeLeadingZero = input => input.filter((lastValue => value => lastValue = lastValue || value) (false) ); console.log(removeLeadingZero([10, 0, 12, 0, 0])); console.log(removeLeadingZero([0, ...
Read MoreHow to add and remove names on button click with JavaScript?
JavaScript allows you to dynamically add and remove names from a list using DOM manipulation methods. This functionality is commonly used in todo lists, user management interfaces, and interactive forms. HTML Structure First, we need a basic HTML structure with an input field, buttons, and a container for the names list: Add and Remove Names body { ...
Read MoreConvert a string to hierarchical object - JavaScript
Let's say, we have a special kind of string that contains characters in couples, like this: const str = "AABBCCDDEE"; console.log(str); AABBCCDDEE We are required to construct an object based on this string which should look like this: const obj = { code: "AA", sub: { code: "BB", sub: { code: "CC", ...
Read MoreConditionally change object property with JavaScript?
To conditionally change object properties in JavaScript, you can use the logical AND operator (&&) combined with the spread operator. This approach allows you to merge properties into an object only when a condition is true. How It Works The logical AND operator returns the second operand if the first is truthy, or false if the first is falsy. When spreading false into an object, it has no effect, making it perfect for conditional property assignment. Syntax let newObject = { ...originalObject, ...condition && { propertyName: value ...
Read MoreFind the number of times a value of an object property occurs in an array with JavaScript?
When working with arrays of objects, you often need to count how many times a specific property value appears. JavaScript's reduce() method combined with Map provides an efficient solution for this task. Using reduce() with Map The reduce() method processes each array element to build a frequency count. We use a Map to store the counts because it maintains insertion order and provides efficient lookups. const subjectDetails = [ { subjectId: '101', subjectName: 'JavaScript' ...
Read MoreCompare array elements to equality - JavaScript
In JavaScript, you can compare array elements at corresponding positions to count how many values match. This is useful for analyzing similarities between two arrays in a sequence-dependent manner. For example, if you have two arrays: const arr1 = [4, 7, 4, 3, 3, 3, 7, 6, 5]; const arr2 = [6, 5, 4, 5, 3, 2, 5, 7, 5]; The function should compare arr1[0] with arr2[0], arr1[1] with arr2[1], and so on. In this case, positions 2, 4, and 7 have matching values, so the result is 3. Using a For Loop ...
Read MoreJoin Map values into a single string with JavaScript?
In JavaScript, a Map stores key-value pairs where each key is unique. To join all Map values into a single string, you can use Array.from() to convert Map values to an array, then apply join() methods. Basic Approach The process involves three steps: extract values from the Map, flatten nested arrays, and join them into a string. let queryStringAppendWithURL = new Map(); queryStringAppendWithURL.set("firstParameter", ["name=John", "age=23", "countryName=US"]); queryStringAppendWithURL.set("secondParameter", ["subjectName=JavaScript", "Marks=91"]); let appendValue = Array.from(queryStringAppendWithURL.values()) .map(value => value.join('&')) .join('&'); console.log("The appended value is: " + appendValue); ...
Read MoreGetting HTML form values and display on console in JavaScript?
In JavaScript, you can retrieve HTML form values using the value property of form elements. This is essential for processing user input and form data. Basic Syntax To get a form element's value, use: document.getElementById("elementId").value Example: Getting Input Value Here's how to get a text input value and display it in the console: Get Form Values ...
Read MoreHow to run a function after two async functions complete - JavaScript
When working with multiple asynchronous operations in JavaScript, you often need to wait for all of them to complete before executing a final function. This is a common scenario in web development where you might be fetching data from multiple APIs or processing several files concurrently. There are several approaches to handle this challenge, with Promise.all() being the most efficient for running multiple async operations concurrently. Using Promise.all() (Recommended) Promise.all() executes multiple promises concurrently and waits for all of them to resolve. It's the optimal choice when you need all operations to complete regardless of their individual ...
Read More