AmitDiwan has Published 10740 Articles

Group objects by property in JavaScript

AmitDiwan

AmitDiwan

Updated on 24-Nov-2020 10:21:21

856 Views

Suppose, we have an array of objects that contains data about some fruits and vegetables like this −const arr = [    {food: 'apple', type: 'fruit'},    {food: 'potato', type: 'vegetable'},    {food: 'banana', type: 'fruit'}, ];We are required to write a JavaScript function that takes in one such array.Our ... Read More

Finding the longest string in an array in JavaScript

AmitDiwan

AmitDiwan

Updated on 24-Nov-2020 10:20:17

581 Views

We are required to write a JavaScript function that takes in an array of strings. Our function should iterate through the array and find and return the longest string from the array.Our function should do this without changing the content of the input array.ExampleThe code for this will be −const ... Read More

Sum identical elements within one array in JavaScript

AmitDiwan

AmitDiwan

Updated on 24-Nov-2020 10:18:54

306 Views

We are required to write a JavaScript function that takes in an array of Numbers.The array might contain some repeating / duplicate entries within it. Our function should add all the duplicate entries and return the new array thus formed.ExampleThe code for this will be −const arr = [20, 20, ... Read More

Grouping an Array and Counting items creating new array based on Groups in JavaScript

AmitDiwan

AmitDiwan

Updated on 24-Nov-2020 10:17:54

366 Views

Suppose, we have an array of objects like this −const arr = [    { region: "Africa", fruit: "Orange", user: "Gary" },    { region: "Africa", fruit: "Apple", user: "Steve" },    { region: "Europe", fruit: "Orange", user: "John" },    { region: "Europe", fruit: "Apple", user: "bob" },   ... Read More

Convert JSON array into normal json in JavaScript

AmitDiwan

AmitDiwan

Updated on 24-Nov-2020 10:16:19

786 Views

Suppose, we have a JSON array with key/value pair objects like this −const arr = [{    "key": "name",    "value": "john" }, {    "key": "number",    "value": "1234" }, {    "key": "price",    "value": [{       "item": [{          "item": [{   ... Read More

Build tree array from flat array in JavaScript

AmitDiwan

AmitDiwan

Updated on 24-Nov-2020 10:08:13

3K+ Views

We have a complex json file that we have to handle with JavaScript to make it hierarchical, in order to later build a tree.Every entry of the JSON array has −id − a unique id, parentId − the id of the parent node (which is 0 if the node is ... Read More

Transform data from a nested array to an object in JavaScript

AmitDiwan

AmitDiwan

Updated on 24-Nov-2020 10:03:29

811 Views

Suppose, we have the following array of arrays −const arr = [    [       ['dog', 'Harry'], ['age', 2]    ],    [       ['dog', 'Roger'], ['age', 5]    ] ];We are required to write a JavaScript function that takes in one such nested array. The ... Read More

Counting the occurrences of JavaScript array elements and put in a new 2d array

AmitDiwan

AmitDiwan

Updated on 24-Nov-2020 10:01:54

457 Views

We are required to write a JavaScript function that takes in an array of literal values. The function should then count the frequency of each element of the input array and prepare a new array on that basis.For example − If the input array is −const arr = [5, 5, ... Read More

Test for existence of nested JavaScript object key in JavaScript

AmitDiwan

AmitDiwan

Updated on 24-Nov-2020 10:00:30

963 Views

Suppose, we have a reference to an object −let test = {};This object will potentially (but not immediately) have nested objects, something like −test = {level1: {level2: {level3: "level3"}}};We are required to write a JavaScript function that takes in one such object as the first argument and then any number ... Read More

Counting number of vowels in a string with JavaScript

AmitDiwan

AmitDiwan

Updated on 24-Nov-2020 09:57:09

707 Views

We are required to write a JavaScript function that takes in a string. The function should count the number of vowels present in the string.The function should prepare an object mapping the count of each vowel against them.ExampleThe code for this will be −const str = 'this is an example ... Read More

Advertisements