Found 6686 Articles for Javascript

How to select the middle of an array?  - JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:55:02

485 Views

We are required to write a JavaScript function that takes in an array of Numbers.The function should return the middlemost element of the array.For example −If the array is −const arr = [1, 2, 3, 4, 5, 6, 7];Then the output should be 4ExampleFollowing is the code −const arr = [1, 2, 3, 4, 5, 6, 7]; const middle = function(){    const half = this.length >> 1;    const offset = 1 - this.length % 2;    return this.slice(half - offset, half + 1); }; Array.prototype.middle = middle; console.log(arr.middle()); console.log([1, 2, 3, 4, 5, 6].middle());OutputThis will produce the following output on console −[ 4 ] [ 3, 4 ]

Convert array into array of subarrays - JavaScript

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

870 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

3K+ 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

254 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

336 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

161 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 - JavaScript

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

537 Views

Suppose, we have three JavaScript arrays of Number like these −const array1 = [10, 23, 53, 74, 33, 56, 6, 0, 43, 45, 11]; const array2 = [52, 46, 27, 28, 4, 11, 53, 6, 75, 75, 22]; const array3 = [26, 18, 10, 12, 31, 12, 5, 8, 44, 34, 65];The length of all the arrays will always be the same.We are required to write a JavaScript function that in any number of such arrays maps the corresponding elements of an existing array according to their rank (i.e., their order in decreasing sense).Therefore, for the above arrays, the output ... Read More

Advertisements