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 109 of 534
Removing property from a JSON object in JavaScript
A JSON (JavaScript Object Notation) object is a data structure surrounded by curly braces {}. JSON objects contain key-value pairs where keys must be strings and values can be numbers, strings, objects, booleans, arrays, or null. JavaScript provides several methods to remove properties from objects. Each method has different characteristics - some modify the original object, while others create new objects without the unwanted properties. Basic JSON Object Structure Here's the basic syntax for a JSON object: const jsonObject = { "name": "John", "age": 30, "city": ...
Read MoreBest way to find length of JSON object in JavaScript
In JavaScript, objects don't have a built-in length property like arrays. However, there are several reliable methods to find the number of properties in a JSON object. Input-Output Scenario Consider an object with some keys and values. We need to get the count of its properties: const MyObj = { Name: "Mike", Age: 34 }; console.log(Object.keys(MyObj).length); // Output: 2 Using Object.keys() (Recommended) The Object.keys() method returns an array of a given object's own enumerable property names. It returns the keys in the same order as they ...
Read MoreCounting smaller and greater in JavaScript
In JavaScript, we often need to count elements in an array that are greater than or smaller than a specific value. This is useful for data analysis, filtering, and statistical operations. Let's say we have an array of numbers and want to count how many elements are greater than and smaller than a given number n. const arr = [3, 5, 5, 2, 23, 4, 7, 8, 8, 9]; console.log("Array:", arr); Array: [3, 5, 5, 2, 23, 4, 7, 8, 8, 9] Using reduce() Method The most elegant approach uses the ...
Read MoreIs element repeated more than n times in JavaScript
In JavaScript, you may need to check if any element appears more than a specified number of times in an array. This is useful for data validation, duplicate detection, and enforcing constraints on array contents. Problem Definition We need to write a function that takes two arguments: An Array of literals that may contain repeating elements A number representing the maximum allowed occurrences (limit) The function should return false if any element appears more than the limit, and true otherwise. Using reduce() and every() Methods The ...
Read MoreFinding the balance of brackets in JavaScript
Given a string that consists of only two types of characters: "(" and ")". We are required to write a function that takes in one such string and balances the parentheses by inserting either a "(" or a ")" as many times as necessary. The function should then return the minimum number of insertions made in the string to balance it. For example: If the string is − const str = '()))'; Then the output should be 2, because by prepending '((' we can balance the string. How It Works The algorithm uses ...
Read MoreSort array of objects by string property value in JavaScript
The given task is to sort an array of objects by using string property value in JavaScript. Assume there is an array of objects and we need to sort those elements by using the string property. In this below scenario, we are sorting array of objects by "Company" property. const array = [ {Company: 'Oneplus', Manufacturing: 'China'}, {Company: 'Samsung', Manufacturing: 'South korea'}, {Company: 'Nothing', Manufacturing: 'India'} ]; // Output after sorting by "Company" property: [ {"Company":"Nothing", "Manufacturing":"India"}, {"Company":"Oneplus", "Manufacturing":"China"}, ...
Read MorePush specific elements to last in JavaScript
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}, {flag: true, other: 6}, {flag: false, other: 7} ]; We are required to write a JavaScript function that takes in one such array and sorts it based on the following conditions: If arr.flag === false, the matching ...
Read MoreGreatest number in a dynamically typed array in JavaScript
We are required to write a JavaScript function that takes in an array that contains some numbers, some strings and some false values. Our function should return the biggest Number from the array. For example: If the input array is − const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii']; Then the output should be 65. Using Math.max with Array Filtering The most straightforward approach is to filter valid numbers first, then use Math.max(): const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii']; ...
Read MoreRemoving Negatives from Array in JavaScript
In JavaScript, you often need to remove negative values from an array. This article shows three effective methods to accomplish this task. Input-Output Scenarios Let's look at typical scenarios. When an array contains both negative and positive values, we need to filter out the negatives: Input = [-2, 5, -7, 32, 78, -32] Output = [5, 32, 78] If the array contains only positive values, it remains unchanged: Input = [56, 43, 12, 67, 69, 34] Output = [56, 43, 12, 67, 69, 34] Using the filter() Method (Recommended) ...
Read MoreJoin arrays to form string in JavaScript
JavaScript provides built-in methods to join array elements into strings. The join() method converts a single array to string, while concat() combined with join() handles multiple arrays. Input-Output Scenarios Converting a single array to string: Input = [32, 45, 65, 12, 07, 55]; Output = "32, 45, 65, 12, 7, 55" // String Joining multiple arrays into a single string: Array1 = [123, 453, 656, 654, 125, 757]; Array2 = ["Hello", "honey", "bunny"]; Output = "123, 453, 656, 654, 125, 757, Hello, honey, bunny" // String Using Array.join() Method ...
Read More