AmitDiwan has Published 10744 Articles

Build tree array from JSON in JavaScript

AmitDiwan

AmitDiwan

Updated on 24-Nov-2020 10:25:34

3K+ Views

Suppose, we have the following array in JavaScript −const arr = [{    "code": "2",    "name": "PENDING" }, {    "code": "2.2",    "name": "PENDING CHILDREN" }, {    "code": "2.2.01.01",    "name": "PENDING CHILDREN CHILDREN" }, {    "code": "2.2.01.02",    "name": "PENDING CHILDREN CHILDREN02" }, {   ... Read More

Group objects by property in JavaScript

AmitDiwan

AmitDiwan

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

824 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

562 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

269 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

310 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

744 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

792 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

431 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

906 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

Advertisements