Found 6683 Articles for Javascript

How to process JavaScript nested array and display the order of numbers according to the level upto which they are nested?

AmitDiwan
Updated on 20-Nov-2020 13:48:59

233 Views

Suppose, we have a nested array of numbers like this −const arr = [23, 6, [2, [6, 2, 1, 2], 2], 5, 2];We are required to write a program that should print the numbers (elements) of this array to the screen.The printing order of the numbers should according to the level upto which they are nested.Therefore, the output for the above input should look like this −23 6    2       6       2       1       2    2 5 2ExampleThe code for this will be −     ... Read More

Take in array of objects and convert the above JSON to a Tree-structure in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:46:34

1K+ Views

Suppose, we have an array of objects like this −const arr = [    {       "parentIndex": '0' ,       "childIndex": '3' ,       "parent": "ROOT",       "child": "root3"    },    {       "parentIndex": '3' ,       "childIndex": '2' ,       "parent": "root3" ,       "child": "root2"    },    {       "parentIndex": '3' ,       "childIndex": '1' ,       "parent": "root3" ,       "child": "root1"    } ];We are required to write a JavaScript ... Read More

Merge arrays in column wise to another array in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:43:15

609 Views

Suppose, we have three arrays of numbers like these −const code = [123, 456, 789]; const year = [2013, 2014, 2015]; const period = [3, 4, 5];We are required to write a JavaScript function that takes in three such arrays. The function should then construct an array of objects based on these three arrays like this −const output = [    {"code": 123, "year": 2013, "period": 3},    {"code": 456, "year": 2014, "period": 4},    {"code": 789, "year": 2015, "period": 5} ];ExampleThe code for this will be −const code = [123, 456, 789]; const year = [2013, 2014, 2015]; const ... Read More

Loop through an index of an array to search for a certain letter in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:42:19

866 Views

We are required to write a JavaScript function that takes in an array of strings as the first argument and a single character as the second argument.The function should return true if the character specified by second argument exists in any of the strings of the array, false otherwise.ExampleThe code for this will be −const arr = ['first', 'second', 'third', 'last']; const searchForLetter = (arr = [], letter = '') => {    for(let i = 0; i < arr.length; i++){       const el = arr[i];       if(!el.includes(letter)){          continue;       ... Read More

Count by unique key in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:40:57

531 Views

Suppose, we have an array of objects like this −const arr = [    {       assigned_user:{          name:'Paul',          id: 34158       },       doc_status: "processed"    },    {       assigned_user:{          name:'Simon',          id: 48569       },       doc_status: "processed"    },    {       assigned_user:{          name:'Simon',          id: 48569       },       doc_status: "processed"    } ];We are required ... Read More

Check if user inputted string is in the array in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:38:41

402 Views

We are required to write a JavaScript program that provides the user an input to enter a string value.The program should then check the input value against some hard-coded array values. Our program should print true to the screen if the input string value is included in the array, false otherwise.ExampleThe code for this will be −            CHECK EXISTENCE           const arr = ['arsenal', 'chelsea', 'everton', 'fulham',       'swansea'];       const checkExistence = () => {          const userInput = document.getElementById("input").value;          const exists = arr.includes(userInput);          document.getElementById('result').innerText = exists;       };            Check     OutputAnd the output on the screen will be −

Reduce an array to groups in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:37:04

162 Views

Suppose, we have an array of strings that contains some duplicate entries like this −const arr = ['blue', 'blue', 'green', 'blue', 'yellow', 'yellow', 'green'];We are required to write a JavaScript function that takes in one such array. The function should merge all the duplicate entries with one another.Therefore, the output for the above input should look like this −const output = ['blueblue', 'green', 'blue', 'yellowyellow', 'green'];ExampleThe code for this will be −const arr = ['blue', 'blue', 'green', 'blue', 'yellow', 'yellow', 'green']; const combineDuplicate = (arr = []) => {    let prev = null;    const groups = arr.reduce((acc, value) ... Read More

Filter array of objects whose properties contains a value in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:35:51

816 Views

Suppose, we have an array of objects like this −const arr = [{    name: 'Paul',    country: 'Canada', }, {    name: 'Lea',    country: 'Italy', }, {    name: 'John',    country: 'Italy', }, ];We are required to devise a way to filter an array of objects depending on a string keyword. The search has to be made in any properties of the object.For instance −When we type "lea", we want to go through all the objects and all their properties to return the objects that contain "lea". When we type "italy", we want to go through all ... Read More

Remove duplicates and map an array in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:32:37

503 Views

Suppose, we have an array of objects like this −const arr = [    {id:123, value:"value1", name:"Name1"},    {id:124, value:"value2", name:"Name1"},    {id:125, value:"value3", name:"Name2"},    {id:126, value:"value4", name:"Name2"} ];Note that some of the "name" property in objects within the array are duplicate.We are required to write a JavaScript function that takes in one such array of objects. The function should then construct a new array of strings that contains only unique "name" property value from the array.Therefore, the output for the above input should look like this −const output = ["Name1", "Name2"];ExampleThe code for this will be −const arr ... Read More

How to group an array of objects by key in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:31:17

890 Views

Suppose, we have an array of objects containing data about some cars like this −const arr = [    {       'make': 'audi',       'model': 'r8',       'year': '2012'    }, {       'make': 'audi',       'model': 'rs5',       'year': '2013'    }, {       'make': 'ford',       'model': 'mustang',       'year': '2012'    }, {       'make': 'ford',       'model': 'fusion',       'year': '2015'    }, {       'make': 'kia',       ... Read More

Advertisements