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
Object Oriented Programming Articles
Page 132 of 589
Length of a JavaScript associative array?
In JavaScript, arrays don't truly support associative arrays (key-value pairs with string keys). When you assign string keys to an array, they become object properties, not array elements, so the length property returns 0. To get the count of properties, use Object.keys(). The Problem with Array.length When you add string keys to an array, they don't count as array elements: var details = new Array(); details["Name"] = "John"; details["Age"] = 21; details["CountryName"] = "US"; details["SubjectName"] = "JavaScript"; console.log("Array length:", details.length); // 0, not 4! console.log("Type:", typeof details); Array length: 0 Type: ...
Read MoreMerge two arrays with alternating Values in JavaScript
Merging two arrays with alternating values means creating a new array that picks elements from each array in turn: first element from array1, first element from array2, second element from array1, and so on. Using While Loop with Index Tracking This approach uses separate counters to track positions in each array and alternates between them: const arr1 = [34, 21, 2, 56, 17]; const arr2 = [12, 86, 1, 54, 28]; let run = 0, first = 0, second = 0; const newArr = []; while(run < arr1.length + arr2.length){ if(first ...
Read MorePossible to split a string with separator after every word in JavaScript
To split a string with separator after every word, you can use the split() method combined with filter() to remove empty elements that may result from consecutive separators. Syntax let result = string.split('separator').filter(value => value); Basic Example Let's start with a string that has separators between words: let sentence = "-My-Name-is-John-Smith-I-live-in-US"; console.log("Original string:", sentence); let result = sentence.split('-').filter(value => value); console.log("After split():"); console.log(result); Original string: -My-Name-is-John-Smith-I-live-in-US After split(): [ 'My', 'Name', 'is', 'John', 'Smith', 'I', 'live', ...
Read MoreHighest and lowest in an array JavaScript
In JavaScript, finding the difference between the highest and lowest values in an array is a common task. This article demonstrates multiple approaches to calculate this difference efficiently. Using Math.max() and Math.min() with Spread Operator The most straightforward approach uses the spread operator with Math.max() and Math.min(): const arr = [23, 54, 65, 76, 87, 87, 431, -6, 22, 4, -454]; const arrayDifference = (arr) => { const max = Math.max(...arr); const min = Math.min(...arr); return max - min; }; console.log("Array:", arr); ...
Read MoreWhat is the simplest solution to flat a JavaScript array of objects into an object?
Flattening an array of objects into a single object combines all key-value pairs from the array elements. The simplest solution uses the reduce() method with the spread operator. Problem Example Consider an array of objects where each object contains different properties: const studentDetails = [ {Name: "Chris"}, {Age: 22} ]; console.log("Original array:", studentDetails); Original array: [ { Name: 'Chris' }, { Age: 22 } ] Using reduce() with Spread Operator The reduce() method iterates through the array and combines all objects into ...
Read MoreSorting objects according to days name JavaScript
Let's say, we have an array of objects that contains data about the humidity over the seven days of a week. The data, however, sits randomly in the array right now. We are supposed to sort the array of objects according to the days like data for Monday comes first, then Tuesday, Wednesday and lastly Sunday. Sample Data Following is our array: const weather = [{ day: 'Wednesday', humidity: 60 }, { day: 'Saturday', humidity: 50 }, { ...
Read MoreHow to determine if date is weekend in JavaScript?
In JavaScript, you can determine if a date falls on a weekend by using the getDay() method. This method returns 0 for Sunday and 6 for Saturday, making weekend detection straightforward. How getDay() Works The getDay() method returns a number representing the day of the week: 0 = Sunday 1 = Monday 2 = Tuesday 3 = Wednesday 4 = Thursday 5 = Friday 6 = Saturday Basic Weekend Check Here's how to check if a specific date is a weekend: var givenDate = new Date("2020-07-18"); var currentDay = givenDate.getDay(); var ...
Read MoreRemove number properties from an object JavaScript
In JavaScript, we often need to filter object properties based on their data types. This article demonstrates how to remove properties of a specific type from an object using a reusable function. Problem Statement Given an object with mixed property types (numbers, strings, booleans, objects), we need to create a function that removes all properties of a specified data type. If no type is specified, it should default to removing number properties. Solution We'll create a function called shedData that iterates through object properties and deletes those matching the specified type: const obj = ...
Read MoreHow can I get seconds since epoch in JavaScript?
To get seconds since epoch in JavaScript, you can use Date.getTime() which returns milliseconds since January 1, 1970 (Unix epoch), then divide by 1000 to convert to seconds. Syntax var date = new Date(); var epochSeconds = Math.round(date.getTime() / 1000); Method 1: Using Math.round() The most common approach uses Math.round() to handle decimal precision: var currentDate = new Date(); var epochSeconds = Math.round(currentDate.getTime() / 1000); console.log("Seconds since epoch:", epochSeconds); console.log("Type:", typeof epochSeconds); Seconds since epoch: 1594821507 Type: number Method 2: Using Math.floor() For exact truncation without ...
Read MoreCheck if object contains all keys in JavaScript array
We are required to write a function containsAll() that takes in two arguments, first an object and second an array of strings. It returns a boolean based on the fact whether or not the object contains all the properties that are mentioned as strings in the array. So, let's write the code for this. We will iterate over the array, checking for the existence of each element in the object, if we found a string that's not a key of object, we exit and return false, otherwise we return true. Example const obj = { ...
Read More