Nested Collection Filter with JavaScript

AmitDiwan
Updated on 24-Nov-2020 10:28:17

1K+ Views

Suppose, we have an array of nested objects like this −const arr = [{    id: 1,    legs:[{       carrierName:'Pegasus'    }] }, {    id: 2,    legs:[{       carrierName: 'SunExpress'    },    {       carrierName: 'SunExpress'    }] }, {    id: 3,    legs:[{       carrierName: 'Pegasus'    },    {       carrierName: 'SunExpress'    }] }];We are required to write a JavaScript function that takes one such array as the first argument and a search query string as the second argument.Our function should filter ... Read More

Build Tree Array from JSON in JavaScript

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" }, {    "code": "1",    "name": "ACTIVE" }, {    "code": "1.1",    "name": "ACTIVE CHILDREN" }, {    "code": "1.1.01",    "name": "ACTIVE CHILDREN CHILDREN" }];We are required to write a JavaScript function that takes in one such array. The function should build a tree structure from this array based on ... Read More

Group Objects by Property in JavaScript

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

826 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 function should then group the array objects based on the "type" property of the objects.It means that all the "fruit" type objects are grouped together and the "vegetable' type grouped together separately.ExampleThe code for this will be −const arr = [    {food: 'apple', type: 'fruit'},    {food: 'potato', type: ... Read More

Finding the Longest String in an Array in JavaScript

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

565 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 arr = ["aaaa", "aa", "aa", "aaaaa", "acc", "aaaaaaaa"]; const findLargest = (arr = []) => {    if(!arr?.length){       return '';    };    let res = '';    res = arr.reduce((acc, val) => {       return acc.length >= val.length ? acc : val;    });    return res; }; console.log(findLargest(arr));OutputAnd the output in the console will be −aaaaaaaa

Sum Identical Elements Within One Array in JavaScript

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

272 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, 20, 10, 10, 5, 1]; const sumIdentical = (arr = []) => {    let map = {};    for (let i = 0; i < arr.length; i++) {       let el = arr[i];       map[el] = map[el] ? map[el] + 1 : 1;    };    const res = [];    for (let count in map) {       res.push(map[count] * count);    };    return res; }; console.log(sumIdentical(arr));OutputAnd the output in the console will be −[ 1, 5, 20, 60 ]

Group and Count Items in an Array in JavaScript

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

317 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" },    { region: "Asia", fruit: "Orange", user: "Ian" },    { region: "Asia", fruit: "Apple", user: "Angelo" },    { region: "Africa", fruit: "Orange", user: "Gary" } ];We are required to write a JavaScript function that takes in one such array. The function should prepare a new array of objects that ... Read More

Convert JSON Array into Normal JSON in JavaScript

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

745 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": [{             "key": "quantity",             "value": "20"          },          {             "key": "price",             "value": "200"          }]       }] ... Read More

Build Tree Array from Flat Array in JavaScript

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 a root of the tree)level − the level of depth in the treeThe JSON data is already "ordered", means that an entry will have above itself a parent node or brother node, and under itself a child node or a brother node.The input array is −const arr = [   ... Read More

Transform Data from a Nested Array to an Object in JavaScript

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

793 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 function should then prepare an object based on the array.The object for the above array should look like −const output = [    {dog: 'Harry', age: 2},    {dog: 'Roger', age: 5} ];ExampleThe code for this will be −const arr = [    [       ['dog', 'Harry'], ['age', ... Read More

Count Occurrences of JavaScript Array Elements in a New 2D Array

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

434 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, 5, 2, 2, 2, 2, 2, 9, 4];Then the output should be −const output = [    [5, 3],    [2, 5],    [9, 1],    [4, 1] ];ExampleThe code for this will be −const arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4]; const frequencyArray = (arr = []) => {    const res = [];    arr.forEach(el => {       if (!this[el]) {          this[el] = [el, 0];          res.push(this[el])       };       this[el][1] ++    }, {});    return res; }; console.log(frequencyArray(arr));OutputAnd the output in the console will be −[ [ 5, 3 ], [ 2, 5 ], [ 9, 1 ], [ 4, 1 ] ]

Advertisements