AmitDiwan has Published 10744 Articles

Add values of matching keys in array of objects - JavaScript

AmitDiwan

AmitDiwan

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

656 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 ... Read More

Sorting objects by numeric values - JavaScript

AmitDiwan

AmitDiwan

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

462 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, ... Read More

Convert JS array into an object - JavaScript

AmitDiwan

AmitDiwan

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

332 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 ... Read More

Sum arrays repeated value - JavaScript

AmitDiwan

AmitDiwan

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

128 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 ... Read More

Group values on same property - JavaScript

AmitDiwan

AmitDiwan

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

317 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 ... Read More

Checking an array for palindromes - JavaScript  

AmitDiwan

AmitDiwan

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

970 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 ... Read More

Fetching JavaScript keys by their values - JavaScript

AmitDiwan

AmitDiwan

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

193 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 ... Read More

Splitting strings based on multiple separators - JavaScript

AmitDiwan

AmitDiwan

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

602 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 ... Read More

Convert number to tens, hundreds, thousands and so on - JavaScript

AmitDiwan

AmitDiwan

Updated on 18-Sep-2020 13:11:59

1K+ Views

We are required to write a function that, given a number, say, 123, will output an array −[100, 20, 3]Basically, the function is expected to return an array that contains the place value of all the digits present in the number taken as an argument by the function.We can solve ... Read More

Splitting an array based on its first value - JavaScript

AmitDiwan

AmitDiwan

Updated on 18-Sep-2020 13:10:51

352 Views

Suppose we have an array of arrays of numbers like this −const arr = [[1, 45], [1, 34], [1, 49], [2, 34], [4, 78], [2, 67], [4, 65]];Each subarray is bound to contain strictly two elements. We are required to write a function that constructs a new array where all ... Read More

Advertisements