Found 10483 Articles for Web Development

Sort array based on presence of fields in objects JavaScript

AmitDiwan
Updated on 20-Aug-2020 06:06:19

278 Views

Let’s say we have the following array of objects −const people = [{    firstName: 'Ram',    id: 301 }, {    firstName: 'Shyam',    lastName: 'Singh',    id: 1016 }, {    firstName: 'Dinesh',    lastName: 'Lamba',    id: 231 }, {    id: 341 }, {    firstName: 'Karan',    lastName: 'Malhotra',    id: 441 }, {    id: 8881 }, {    firstName: 'Vivek',    id: 301 }];We are required to sort this array so that the object with both firstName and lastName property appears first then the objects with firstName or lastName and lastly the objects ... Read More

Get closest number out of array JavaScript

Nikitasha Shrivastava
Updated on 14-Aug-2023 18:01:38

562 Views

In the above problem statement we are required to get the closest number of the given target out of the array. And we have to produce the code with the help of Javascript. Understanding the Problem The problem at hand is to find the closest number of the given target value within the array. So this problem can be achieved using a logical algorithm which will iterate through the array and compare every item to the target value. And then we will determine the closest number. So we will use Javascript to develop the solution. ... Read More

Find all subarrays with sum equal to number? JavaScript (Sliding Window Algorithm)

AmitDiwan
Updated on 20-Aug-2020 06:01:40

609 Views

We are given an array of numbers and a number; our job is to write a function that returns an array of all the sub arrays which add up to the number provided as second argument.For example −const arr = [23, 5, 1, 34, 12, 67, 9, 31, 6, 7, 27]; const sum = 40; console.log(requiredSum(arr, sum));Should output the following array −[ [ 5, 1, 34 ], [ 9, 31 ], [ 6, 7, 27 ] ]Because each of these 3 sub arrays add up to 40.The Sliding Window Algorithm (Linear Time)This algorithm is mostly used when we are required ... Read More

Algorithm to dynamically populate JavaScript array with zeros before and after values

AmitDiwan
Updated on 20-Aug-2020 05:59:48

149 Views

We are given a months array, which elements less than 12, where each element will be between 1 and 12 (both inclusive). Our job is to take this array and create a full months array with 12 elements, if the element is present in the original array we use that element otherwise we use at that place.For example −Intput → [5, 7, 9] Output → [0, 0, 0, 0, 5, 0, 7, 0, 9, 10, 0, 0]Now, let’s write the code −Exampleconst months = [6, 7, 10, 12]; const completeMonths = (arr) => {    const completed = [];    for(let i = 1; i

JavaScript remove random item from array and then remove it from array until array is empty

Alshifa Hasnain
Updated on 13-Dec-2024 13:53:44

3K+ Views

We are given an array of string/number literals. We are required to create a function removeRandom() that takes in the array, recursively removes one random item from it, and simultaneously prints it until the array contains items. This can be done by creating a random number using Math.random(), removing the item at that index using Array.prototype.splice(), and printing it until the length of the array shrinks to 0. Approaches to remove random items from array Following are the different approaches to removing the random item from an array and then removing it from the array until ... Read More

JavaScript Count characters case insensitive

Disha Verma
Updated on 11-Mar-2025 16:38:27

571 Views

Counting characters in a string is a common task in programming, especially when analyzing text or creating applications that handle user input. Counting characters in a string without worrying about uppercase or lowercase letters is a common task in text processing and data analysis. JavaScript offers different methods to easily count how many times each character appears, regardless of case. Understanding Case Insensitive Case insensitivity in programming means that a system takes uppercase and lowercase letters as the same. For example, "Hello" and "hello" would be treated as equal in a case-insensitive situation. Case-Insensitive Character Counting You can ... Read More

Check if object contains all keys in JavaScript array

AmitDiwan
Updated on 19-Aug-2020 07:23:48

529 Views

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.Here is the code for doing the same −Exampleconst obj = { ... Read More

JavaScript map value to keys (reverse object mapping)

Alshifa Hasnain
Updated on 24-Dec-2024 17:45:53

1K+ Views

In JavaScript, there are scenarios where we need to reverse the mapping of keys and values in an object, creating a new object where the original values become keys, and the keys become their corresponding values. For example, cities can be grouped by their states. In this article, we’ll learn two approaches to achieve this: using Object.keys with iteration and reduce. Iterating with Object.keys() The first approach involves iterating over the keys of the original object using Object.keys() of Object properties and populating a new object. If multiple keys in the original object share the same value, they are grouped into ... Read More

How to get the product of two integers without using * JavaScript

AmitDiwan
Updated on 19-Aug-2020 07:21:19

322 Views

We are required to write a function that takes in two numbers and returns their product, but without using the (*) operator.Trick 1: Using Divide Operator TwiceWe know that multiplication and division are just the inverse of each other, so if we divide a number by other number’s inverse, won’t it be same as multiplying the two numbers?Let’s see the code for this −const a = 20, b = 45; const product = (a, b) => a / (1 / b); console.log(product(a, b));Trick 2: Using LogarithmsLet’s examine the properties of logarithms first −log(a) + log(b) = log(ab)So, let’s use this ... Read More

Remove number properties from an object JavaScript

AmitDiwan
Updated on 19-Aug-2020 07:19:58

276 Views

We are given an object that contains some random properties, including some numbers, boolean, strings and the object itself.We are required to write a function that takes in the object as first argument and a string as second argument, possible value for second argument is a name of any data type in JavaScript like number, string, object, boolean, symbol etc.Our task is to delete every property of type specified by the second argument. If the second argument is not provided, take ‘number’ as default.The full code for doing so will be −const obj = {    name: 'Lokesh Rahul',   ... Read More

Advertisements