Found 8591 Articles for Front End Technology

How to remove blank (undefined) elements from JavaScript array - JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:29:52

698 Views

Suppose we have an array of literals like this −const arr = [4, 6, , 45, 3, 345, , 56, 6];We are required to write a JavaScript function that takes in one such array and remove all the undefined elements from the array in place. We are only required to remove the undefined and empty values and not all the falsy values.Use a for loop to iterate over the array and Array.prototype.splice() to remove undefined elements in place.ExampleFollowing is the code −const arr = [4, 6, , 45, 3, 345, , 56, 6] const eliminateUndefined = arr => {   ... Read More

Sort object array based on another array of keys - JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:28:32

4K+ Views

Suppose, we have two arrays like these −const arr1 = ['d', 'a', 'b', 'c'] ; const arr2 = [{a:1}, {c:3}, {d:4}, {b:2}];We are required to write a JavaScript function that accepts these two arrays. The function should sort the second array according to the elements of the first array.We have to sort the keys of the second array according to the elements of the first array. This will produce the following output −const output = [{d:4}, {a:1}, {b:2}, {c:3}];ExampleFollowing is the code −const arr1 = ['d', 'a', 'b', 'c'] ; const arr2 = [{a:1}, {c:3}, {d:4}, {b:2}]; const sortArray = ... Read More

How to subtract elements of two arrays and store the result as a positive array in JavaScript?

AmitDiwan
Updated on 18-Sep-2020 13:27:01

3K+ Views

Suppose, we have two arrays like these −const arr1 = [1, 2, 3, 4, 5, 6]; const arr2 = [9, 8, 7, 5, 8, 3];We are required to write a JavaScript function that takes in two such arrays and returns an array of absolute difference between the corresponding elements of the array.Therefore, for these arrays, the output should look like −const output = [8, 6, 4, 1, 3, 3];We will use a for loop and keep pushing the absolute difference iteratively into a new array and finally return the array.ExampleFollowing is the code −const arr1 = [1, 2, 3, 4, ... Read More

Generating n random numbers between a range - JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:25:46

257 Views

We are required to write a JavaScript function that takes in a number, say n, and an array of two numbers that represents a range. The function should return an array of n random elements all lying between the range provided by the second argument.ExampleFollowing is the code −const num = 10; const range = [5, 15]; const randomBetween = (a, b) => {    return ((Math.random() * (b - a)) + a).toFixed(2); }; const randomBetweenRange = (num, range) => {    const res = [];    for(let i = 0; i < num; ){       const random ... Read More

Store count of digits in order using JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:24:34

114 Views

Suppose we have a string with digits like this −const str = '11222233344444445666';We are required to write a JavaScript function that takes in this string and returns an object that represents the count of each number in the string.Therefore, for this string, the output should be −const output = {    "1": 2,    "2": 4,    "3": 3,    "4": 7,    "5": 1,    "6": 3 };ExampleFollowing is the code −const str = '11222233344444445666'; const mapString = str => {    const map = {};    for(let i = 0; i < str.length; i++){       map[str[i]] ... Read More

Finding the sum of unique array values - JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:23:33

992 Views

We are required to write a JavaScript function that takes in an array of numbers that may contain some duplicate numbers.Our function should return the sum of all the unique elements (elements that only appear once in the array) present in the array.For example −If the input array is −const arr = [2, 5, 5, 3, 2, 7, 4, 9, 9, 11];Then the output should be 25We will simply use a for loop, iterate the array and return the sum of unique elements.ExampleFollowing is the code −const arr = [2, 5, 5, 3, 2, 7, 4, 9, 9, 11]; const ... Read More

Add values of matching keys in array of objects - JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:22:25

658 Views

Suppose we have an array of objects like this −const arr = [{a: 2, b: 5, c: 6}, {a:3, b: 4, d:1}, {a: 1, d: 2}];Each object is bound to have unique keys in itself (for it to be a valid object), but two different objects can have the common keys (for the purpose of this question)We are required to write a JavaScript function that takes in one such array and returns an object with all the unique keys present in the array and their values cumulative sum as the valueTherefore, the resultant object should look like −const output = ... Read More

Sorting objects by numeric values - JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:21:13

463 Views

Suppose we have an object like this −const obj = {    key1: 56,    key2: 67,    key3: 23,    key4: 11,    key5: 88 };We are required to write a JavaScript function that takes in this object and returns a sorted array like this −const arr = [11, 23, 56, 67, 88];Here, we sorted the object values and placed them in an array.ExampleFollowing is the code −const obj = {    key1: 56,    key2: 67,    key3: 23,    key4: 11,    key5: 88 }; const sortObject = obj => {    const arr = Object.keys(obj).map(el => {       return obj[el];    });    arr.sort((a, b) => {       return a - b;    });      return arr; }; console.log(sortObject(obj));OutputThis will produce the following output in console −[ 11, 23, 56, 67, 88 ]

Convert JS array into an object - JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:19:56

334 Views

Suppose, we have an array of objects like this −const arr = [    {id: 1, name: "Mohan"},    {id: 2, name: "Sohan"},    {id: 3, name: "Rohan"} ];We are required to write a function that takes one such array and constructs an object from it with the id property as key and name as valueThe output for the above array should be −const output = {1:{name:"Mohan"}, 2:{name:"Sohan"}, 3:{name:"Rohan"}}ExampleFollowing is the code −const arr = [    {id: 1, name: "Mohan"},    {id: 2, name: "Sohan"},    {id: 3, name: "Rohan"} ]; const arrayToObject = arr => {    const ... Read More

Sum arrays repeated value - JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:18:44

130 Views

Suppose, we have an array of objects like this −const arr = [    {'ID-01':1},    {'ID-02':3},    {'ID-01':3},    {'ID-02':5} ];We are required to add the values for all these objects together that have identical keysTherefore, for this array, the output should be −const output = [{'ID-01':4}, {'ID-02':8}];We will loop over the array, check for existing objects with the same keys, if they are there, we add value to it otherwise we push new objects to the array.ExampleFollowing is the code −const arr = [    {'ID-01':1},    {'ID-02':3},    {'ID-01':3},    {'ID-02':5} ]; const indexOf = function(key){   ... Read More

Advertisements