Articles on Trending Technologies

Technical articles with clear explanations and examples

Filtering array to contain palindrome elements in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 366 Views

We are required to write a JavaScript function that takes in an array of String / Number literals and returns a subarray of all the elements that were palindrome in the original array. For example If the input array is − const arr = ['carecar', 1344, 12321, 'did', 'cannot']; Then the output should be − const output = [12321, 'did']; We will create a helper function that takes in a number or a string and checks if it's a palindrome or not. Then we will loop over the array, filter ...

Read More

Creating an array of objects based on another array of objects JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 733 Views

In JavaScript, you can create a new array of objects based on an existing array by transforming each object's structure. This is commonly done using the map() method to extract specific properties and create new object formats. Suppose we have an array of objects containing user data like this: const arr = [ {"user":"dan", "liked":"yes", "age":"22"}, {"user":"sarah", "liked":"no", "age":"21"}, {"user":"john", "liked":"yes", "age":"23"}, ]; console.log("Original array:", arr); Original array: [ { user: 'dan', liked: 'yes', age: '22' }, { ...

Read More

Build tree array from flat array in JavaScript

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

Converting a flat array into a hierarchical tree structure is a common task when working with JSON data. This process transforms an array where each item has an id and parentId into a nested structure with parent-child relationships. Every entry in the JSON array has: id — a unique identifier parentId — the id of the parent node (which is "0" for root nodes) level — the depth level in the tree hierarchy The JSON data is already ordered, meaning each entry will ...

Read More

Forming string using 0 and 1 in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 322 Views

We need to write a JavaScript function that takes an array of binary strings and determines the maximum number of strings that can be formed using at most m zeros and n ones. Problem Statement Given an array of strings containing only '0' and '1', find how many strings can be selected such that the total count of zeros doesn't exceed m and the total count of ones doesn't exceed n. For example, with the array ["10", "0001", "111001", "1", "0"] and limits m=5 zeros and n=3 ones: Input: arr = ["10", "0001", "111001", "1", ...

Read More

Converting a Polygon object into a data-like URL string using FabricJS

Rahul Gurung
Rahul Gurung
Updated on 15-Mar-2026 278 Views

We can create a Polygon object by creating an instance of fabric.Polygon. A polygon object can be characterized by any closed shape consisting of a set of connected straight line segments. Since it is one of the basic elements of FabricJS, we can also easily customize it by applying properties like angle, opacity etc. In order to convert a Polygon object into a data-like URL string we use the toDataURL method. This method converts an object into a data-like URL string that represents the visual appearance of the polygon as a base64-encoded image. Syntax toDataURL(options: Object): ...

Read More

How to implement the delete-all operation programmatically using FabricJS?

Rahul Gurung
Rahul Gurung
Updated on 15-Mar-2026 606 Views

We can create a Polygon object by creating an instance of fabric.Polygon. A polygon object can be characterized by any closed shape consisting of a set of connected straight line segments. Since it is one of the basic elements of FabricJS, we can also easily customize it by applying properties like angle, opacity etc. In order to implement delete-all programmatically, we need to use the clear method. This method clears all the contexts of an instance and removes all objects from the canvas. Syntax clear(): fabric.Canvas Parameters The clear() method does not accept any parameters ...

Read More

How to assign a PHP variable to JavaScript?

Rushi Javiya
Rushi Javiya
Updated on 15-Mar-2026 23K+ Views

PHP and JavaScript run at different times in the web development lifecycle. PHP executes on the server before sending HTML to the browser, while JavaScript runs in the browser after receiving the page. This tutorial shows various methods to pass PHP variable values to JavaScript. Understanding the execution sequence is crucial: PHP processes your .php file first to generate HTML, then sends the processed code to the client's browser. Once in the browser, JavaScript executes, but PHP is no longer accessible. Using Echo (Direct Assignment) The simplest method is using PHP's echo to output PHP variables directly ...

Read More

How to make filter and word replacement method - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 336 Views

In JavaScript, there's no built-in method to replace all occurrences of a word in a string. You can create custom functions using methods like split() and join(), or use modern approaches like replaceAll(). Let's explore different methods to filter and replace words in a string: var sentence = "Yes, My Name is John Smith. I live in US. Yes, My Favourite Subject is JavaScript"; Method 1: Using split() and join() This method splits the string by the target word and joins the parts with the replacement: var sentence = "Yes, My Name ...

Read More

JavaScript array sorting by level

Alshifa Hasnain
Alshifa Hasnain
Updated on 15-Mar-2026 1K+ Views

In this article, we will learn array sorting by level in JavaScript, creating a hierarchical tree structure from a flat array is a common challenge when dealing with relational data. This is a common task when dealing with hierarchical data, such as organizational charts, category trees, or file systems, where relationships like parent-child need to be represented. Problem Statement We have an array of objects representing nodes with _id, level, and parentId properties. Our goal is to transform this array into a tree structure where nodes are nested as children under their respective parents. The elements with the ...

Read More

How to read JSON file in JavaScript?

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

There are three effective methods to read JSON files in JavaScript using fetch(), import statements, and require(). This article covers practical examples for both browser and Node.js environments. JSON (JavaScript Object Notation) is a lightweight data interchange format that's easy for humans to read and write. In this article, we'll explore three different approaches to reading JSON files in JavaScript: Approaches to Read JSON file Using the fetch() method (Browser) Using the import statement (ES6 Modules) Using the require() method (Node.js) Each method ...

Read More
Showing 14401–14410 of 61,297 articles
Advertisements