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 183 of 589
Find indexes of multiple minimum value in an array in JavaScript
Suppose we have an array of numbers like this − const arr = [1, 2, 3, 4, 1, 7, 8, 9, 1]; Suppose we want to find the index of the smallest element in the array i.e. 1 above. For this, we can simply use − const min = Math.min.apply(Math, arr); const ind = arr.indexOf(min); The above code will successfully set ind to 0, which indeed is correct. But what we want to achieve is that if there are more than one minimum elements in the array, like in the ...
Read MoreReturning the highest number from object properties value – JavaScript
When working with JavaScript objects, you might need to find the property with the highest value. For example, finding the highest rating from a property rating system. Suppose we have an object that contains ratings of a property over some criteria like this: const rating = { "overall": 92, "atmosphere": 93, "cleanliness": 94, "facilities": 89, "staff": 94, "security": 92, "location": 88, "valueForMoney": 92 } We need to write a JavaScript function ...
Read MoreJavaScript - Convert an array to key value pair
Converting an array of objects to key-value pairs is a common task in JavaScript. This involves transforming structured data into a simple object where one property becomes the key and another becomes the value. Problem Statement Suppose we have an array of student objects like this: const arr = [ {"name": "Rahul", "score": 89}, {"name": "Vivek", "score": 88}, {"name": "Rakesh", "score": 75}, {"name": "Sourav", "score": 82}, {"name": "Gautam", "score": 91}, {"name": "Sunil", ...
Read MoreRemove array duplicates by property - JavaScript
When working with arrays of objects in JavaScript, you often need to remove duplicate entries based on a specific property. This tutorial shows different methods to accomplish this task. The Problem Consider an array of objects with duplicate names: const arr = [ {name: "Jack", age: "14"}, {name: "bob", age: "14"}, {name: "sue", age: "21"}, {name: "Jill", age: "16"}, {name: "Jack", age: "21"} ]; console.log("Original array:"); console.log(arr); Original array: [ ...
Read MoreBuilding a Map from 2 arrays of values and keys in JavaScript
In JavaScript, you can create a Map from two separate arrays containing keys and values. This is useful when you have parallel arrays where each index corresponds to a key-value pair. Problem Setup Suppose we have two arrays: const keys = [0, 4, 2, 3, 1]; const values = ["first", "second", "third", "fourth", "fifth"]; We need to create a Map where each key from the first array maps to the corresponding value from the second array at the same index. Using a for Loop The most straightforward approach is to iterate through ...
Read MoreCounting occurrences of vowel, consonants - JavaScript
We are required to write a JavaScript function that takes in a string which contains English alphabet, for example − const str = 'This is a sample string, will be used to collect some data'; The function should return an object containing the count of vowels and consonants in the string i.e. the output should be − { vowels: 17, consonants: 29 } Understanding the Problem To count vowels and consonants, we need to: Iterate through each character in the string Check if the character ...
Read MoreSorting an array object by property having falsy value - JavaScript
Suppose, we have an array of objects like this − const array = [ {key: 'a', value: false}, {key: 'a', value: 100}, {key: 'a', value: null}, {key: 'a', value: 23} ]; We are required to write a JavaScript function that takes in one such array and places all the objects that have falsy values for the "value" property to the bottom and sorts all other objects in decreasing order by the "value" property. Understanding Falsy Values In JavaScript, falsy values include false, null, undefined, 0, "", ...
Read MoreCheck if a string is sorted in JavaScript
We are required to write a JavaScript function that takes in a string and checks whether it is sorted or not. A string is considered sorted if its characters are arranged in either ascending or descending order. For example: isSorted('adefgjmxz') // true (ascending) isSorted('zxmfdba') // true (descending) isSorted('dsfdsfva') // false (mixed order) Method 1: Character by Character Comparison This approach compares adjacent characters and tracks whether the string is ascending or descending: const str = 'abdfhlmxz'; const findDiff = (a, b) => a.charCodeAt(0) - b.charCodeAt(0); ...
Read MoreCreating an array using a string which contains the key and the value of the properties - JavaScript
Suppose, we have a special kind of string like this − const str = "Integer, 1 Float, 2.0Boolean, True Integer, 6Float, 3.66 Boolean, False"; We are required to write a JavaScript function that converts the above string into an array of objects, using the String.prototype.split() method − const arr = [ { "Integer": 1, "Float": 2.0 }, { ...
Read MoreFiguring out the highest value through a for in loop - JavaScript
When working with comma-separated strings in JavaScript, you might need to find which value appears most frequently. This tutorial demonstrates how to use a for-in loop to count occurrences and determine the most frequent item. Suppose we have a comma-separated string containing fruit names: const str = 'Banana, Banana, Pear, Orange, Apple, Melon, Grape, Apple, Banana, Grape, Melon, Grape, Melon, Apple, Grape, Banana, Orange, Melon, Orange, Banana, Banana, Orange, Pear, Grape, Orange, Orange, Apple, Apple, Banana'; console.log(str); Banana, Banana, Pear, Orange, Apple, Melon, Grape, Apple, Banana, Grape, Melon, Grape, Melon, Apple, Grape, Banana, Orange, ...
Read More