Found 10483 Articles for Web Development

Convert array into array of subarrays - JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:52:32

988 Views

We are required to write a JavaScript function that takes in an array of literals and returns a new array that have elements of the original array chunked into subarrays of length exactly 2.Now if the length of original array is not exactly divisible by 2, then the last subarray should have only one element.For example, If the input array is −const arr = [1, 2, 3, 4, 5, 6, 7];Then the output should be −const output = [[1, 2], [3, 4], [5, 6], [7]]ExampleFollowing is the code −const arr = [1, 2, 3, 4, 5, 6, 7]; const chunk ... Read More

Merge two objects in JavaScript ignoring undefined values

AmitDiwan
Updated on 01-Oct-2020 10:51:13

2K+ Views

Suppose, we have two objects, say A and B like these −const A = { activity: 'purchased', count: undefined, time: '09:05:33' }; const B = { activity: 'purchased', count: '51', time: undefined }; We are required to write a JavaScript function that merges these two objects, keeping in mind if any key has a truthy value then it should not be overwritten by a key having falsy value.If we do this simply by using the spread operator, it will not keep track of truth or falsy values.Therefore, we have to do this using an iterative approach.ExampleFollowing is the code −const A ... Read More

Remove leading zeros in array - JavaScript

Nikhilesh Aleti
Updated on 19-Dec-2022 17:12:28

4K+ Views

In this article, the given task is to remove the leading zeros in an array in JavaScript. Before we dive into the examples, let’s get an understanding of the problem by looking at the input-output scenarios below. Input-Output Scenario Let’s look into some input-output scenarios - Assume that we are having an array with elements in it. In which the starting elements in the array are zeros and task is to remove the leading arrays. Input = [0, 0, 0, 5, 67, 12, 4, 0, 5]; Output = [5, 67, 12, 4, 0, 5] Now, consider another scenario ... Read More

Sort an array to have specific items first in the array - JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:49:03

2K+ Views

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 element gets placed first in the array, but only after the previous matching elements.The elements that do not match, remain in the same order ... Read More

Sort array of objects by string property value - JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:47:18

316 Views

Suppose, we have an array of Objects like this −const arr = [    { first_name: 'Lazslo', last_name: 'Jamf'     },    { first_name: 'Pig',    last_name: 'Bodine'   },    { first_name: 'Pirate', last_name: 'Prentice' } ];We are required to write a JavaScript function that takes in one such array and sort this array according to the alphabetical value of the last_name key.ExampleFollowing is the code −const arr = [    { first_name: 'Lazslo', last_name: 'Jamf' },    { first_name: 'Pig', last_name: 'Bodine' },    { first_name: 'Pirate', last_name: 'Prentice' } ]; const sortByLastName = arr => { ... Read More

Grouping array nested value while comparing 2 objects - JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:45:30

407 Views

Suppose, we have the following JSON object −const input = {    "before": {      "device": [        {          "id": "1234",          "price": "10",          "features": [            {              "name": "samsung",              "price": "10"            },            {              "name": "Apple",              "price": "20"            }         ... Read More

JavaScript: Balancing parentheses

AmitDiwan
Updated on 01-Oct-2020 10:40:21

2K+ Views

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.ExampleFollowing is the code −const str = '()))'; const balanceParanthesis = str => {    let paren ... Read More

Compare and fill arrays - JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:38:44

223 Views

We are required to write a function that compares two arrays and creates a third array filling that array with all the elements of the second array and filling null for all those elements that are present in the first array but misses out in the second array.For example −If the two arrays are −const arr1 = ['f', 'g', 'h']; const arr2 = ['f', 'h'];Then the output should be −const output = ['f', null, 'h'];ExampleFollowing is the code −const arr1 = ['f', 'g', 'h']; const arr2 = ['f', 'h']; const compareAndFill = (arr1, arr2) => {    let offset = ... Read More

JavaScript Array Ranking

Alshifa Hasnain
Updated on 22-Jan-2025 00:46:49

740 Views

In this article, we will learn to rank elements in arrays using JavaScript. Ranking elements in an array is common in various applications, such as sorting scores, evaluating performance, or analyzing data. What is Array Ranking? Array ranking involves assigning a rank to each element of an array based on its value, where the largest value gets the highest rank and the smallest value gets the lowest rank. This ranking can be extended to multiple arrays to analyze and compare data across different datasets. Problem Statement We are given multiple arrays of numbers. The task is to compute ranks for ... Read More

How to validate if an element in an array is repeated? - JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:36:18

296 Views

We are required to write a JavaScript function that takes in two arguments −An Array, say arr, of literals that may contain some repeating elements.A number, say limit.The function should validate that no element of the array is repeated more than limit number of times. If any element is repeated more than the limit the function should return false, true otherwise.ExampleFollowing is the code −const arr = [4, 6, 7, 4, 2, 5, 7, 7, 4, 4, 3]; const validateElements = (arr, n) => {    const counts = arr.reduce((acc, el) => {       acc[el] = (acc[el] + ... Read More

Advertisements