Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Javascript Articles
Page 298 of 534
Deep Search JSON Object JavaScript
Suppose we have the following nested JSON object −const obj = { id: 1, title: 'hello world', child: { id: null, title: 'foobar', child: { id: null, title: 'i should be in results array ' } }, foo: { id: null, title: 'i should be in results array too!' }, deep: [ { id: null, ...
Read MoreReduce array in JavaScript
Suppose, we have an array of objects like this −const arr = [ {"time":"18:00:00"}, {"time":"10:00:00"}, {"time":"16:30:00"} ];We are required to write a JavaScript function that takes in one such array and does the following −Extract the times from the json code: so: 18:00:00, 10:00:00, 16:30:00Convert the times to this: [18,0], [10,0], [16,30]Put it in an array.Return the final array.ExampleThe code for this will be −const arr = [ {"time":"18:00:00"}, {"time":"10:00:00"}, {"time":"16:30:00"} ]; const reduceArray = (arr = []) => { let res = []; res = arr.map(obj => { return obj['time'].split(':').slice(0, 2).map(el => { return +el; }); }); return res; }; console.log(reduceArray(arr));OutputAnd the output in the console will be −[ [ 18, 0 ], [ 10, 0 ], [ 16, 30 ] ]
Read MoreHow to merge two object arrays of different size by key in JavaScript
Suppose, we have an object like this −const obj = { "part1": [{"id": 1, "a": 50}, {"id": 2, "a": 55}, {"id": 4, "a": 100}], "part2":[{"id": 1, "b": 40}, {"id": 3, "b": 45}, {"id": 4, "b": 110}] };We are required to write a JavaScript function that takes in one such object. The function should merge part1 and part2 of the object to form an array of objects like this −const output = [ {"id": 1, "a": 50, "b": 40}, {"id": 2, "a": 55}, {"id": 3, "b": 45}, {"id": 4, "a": 100, "b": 110} ];ExampleThe code ...
Read MoreHow to find inside an array of objects the object that holds the highest value in JavaScript?
We have an array that holds several objects named student, each object student has several properties, one of which is an array named grades −const arr = [ { name: "Student 1", grades: [ 65, 61, 67, 70 ] }, { name: "Student 2", grades: [ 50, 51, 53, 90 ] }, { name: "Student 3", grades: [ 0, 20, 40, 60 ] } ];We need to create a function that loops through the student's array ...
Read MoreHow to process JavaScript nested array and display the order of numbers according to the level upto which they are nested?
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 MoreTake in array of objects and convert the above JSON to a Tree-structure in JavaScript
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 MoreMerge arrays in column wise to another array in JavaScript
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 MoreLoop through an index of an array to search for a certain letter in JavaScript
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 MoreCount by unique key in JavaScript
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 MoreCheck if user inputted string is in the array in JavaScript
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 −
Read More