Found 10483 Articles for Web Development

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

Group values on same property - JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:17:16

320 Views

Suppose we have an array like this −const arr = [    {unit: 35, brand: 'CENTURY'},    {unit: 35, brand: 'BADGER'},    {unit: 25, brand: 'CENTURY'},    {unit: 15, brand: 'CENTURY'},    {unit: 25, brand: 'XEGAR'} ];We are required to write a function that groups all the brand properties of objects whose unit property is the same.Like for the above array, the new array should be −const output = [    {unit: 35, brand: 'CENTURY, BADGER'},    {unit: 25, brand: 'CENTURY, XEGAR'},    {unit: 15, brand: 'CENTURY'} ];We will loop over the array, search for the object with unit value ... Read More

Checking an array for palindromes - JavaScript  

AmitDiwan
Updated on 18-Sep-2020 13:15:59

972 Views

We are required to write a JavaScript function that takes in an array of String / Number literals and returns a subarray of all the elements that were palindrome in the original array.For example −If the input array is −const arr = ['carecar', 1344, 12321, 'did', 'cannot'];Then the output should be −const output = [12321, 'did'];We will create a helper function that takes in a number or a string and checks if it’s a boolean or not. Then we will loop over the array, filter the palindrome elements and return the filtered arrayExampleFollowing is the code −const arr = ['carecar', ... Read More

Fetching JavaScript keys by their values - JavaScript

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

197 Views

Suppose, we have an object like this −const products = {    "Pineapple":38,    "Apple":110,    "Pear":109 };All the keys are unique in themselves and all the values are unique in themselves. We are required to write a function that accepts a value and returns its keyFor example: findKey(110) should return −"Apple"We will approach this problem by first reverse mapping the values to keys and then simply using object notations to find their values.ExampleFollowing is the code −const products = {    "Pineapple":38,    "Apple":110,    "Pear":109 }; const findKey = (obj, val) => {    const res = {}; ... Read More

Splitting strings based on multiple separators - JavaScript

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

604 Views

We are required to write a JavaScript function that takes in a string and any number of characters specified as separators. Our function should return a splitted array of the string based on all the separators specified.For example −If the string is −const str = 'rttt.trt/trfd/trtr, tr';And the separators are −const sep = ['/', '.', ', '];Then the output should be −const output = [ 'rttt', 'trt', 'trfd', 'trtr' ];ExampleFollowing is the code −const str = 'rttt.trt/trfd/trtr, tr'; const splitMultiple = (str, ...separator) => {    const res = [];    let start = 0;    for(let i = 0; ... Read More

Advertisements