- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 6683 Articles for Javascript
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
291 Views
We are required to write a JavaScript function that takes in an array of literal values as the first argument and a string as the second argument.Our function should sort the array alphabetically but keeping the string provided as the second argument (if it exists in the array) as the first element, irrespective of the text it contains.ExampleThe code for this will be −const arr = ["Apple", "Orange", "Grapes", "Pineapple", "None", "Dates"]; const sortKeepingConstants = (arr = [], text = '') => { const sorter = (a, b) => { return (b === text) - (a ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
691 Views
Suppose, we have two child and parent JSON arrays of objects like these −const child = [{ id: 1, name: 'somename', parent: { id: 2 }, }, { id: 2, name: 'some child name', parent: { id: 4 } }]; const parent = [{ id: 1, parentName: 'The first', child: {} }, { id: 2, parentName: 'The second', child: {} }, { id: 3, parentName: 'The third', child: {} }, { id: 4, parentName: 'The ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
692 Views
Suppose we have a sorted array of numbers like this where we can have consecutive numbers.const arr = [1, 2, 3, 5, 7, 8, 9, 11];We are required to write a JavaScript function that takes one such array.Our function should form a sequence for this array. The sequence should be such that for all the consecutive elements of the array, we have to just write the starting and ending numbers replacing the numbers in between with a dash (-), and keeping all other numbers unchanged.Therefore, for the above array, the output should look like −const output = '1-3, 5, 7-9, ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
731 Views
Suppose, we have an array of objects like this −const arr = [ {"Date":"2014", "Amount1":90, "Amount2":800}, {"Date":"2015", "Amount1":110, "Amount2":300}, {"Date":"2016", "Amount1":3000, "Amount2":500} ];We are required to write a JavaScript function that takes in one such array and maps this array to another array that contains arrays instead of objects.Therefore, the final array should look like this −const output = [ ['2014', 90, 800], ['2015', 110, 300], ['2016', 3000, 500] ];ExampleThe code for this will be −const arr = [ {"Date":"2014", "Amount1":90, "Amount2":800}, {"Date":"2015", "Amount1":110, "Amount2":300}, {"Date":"2016", "Amount1":3000, "Amount2":500} ]; const arrify ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
748 Views
Suppose, we have a comma-separated string like this −const str = "a, b, c, d , e";We are required to write a JavaScript function that takes in one such and sheds it off all the whitespaces it contains.Then our function should split the string to form an array of literals and return that array.ExampleThe code for this will be −const str = "a, b, c, d , e"; const shedAndSplit = (str = '') => { const removeSpaces = () => { let res = ''; for(let i = 0; i < str.length; ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
172 Views
We are required to write a JavaScript function that takes in an array of literal values. The array might contain some repeating values inside it.Our function should remove all the repeating values keeping the first instance of repeating value in the array.ExampleThe code for this will be −const arr = [1, 5, 7, 4, 1, 4, 4, 6, 4, 5, 8, 8]; const deleteDuplicate = (arr = []) => { for(let i = 0; i < arr.length; ){ const el = arr[i]; if(i !== arr.lastIndexOf(el)){ arr.splice(i, 1); } else{ i++; }; }; }; deleteDuplicate(arr); console.log(arr);OutputAnd the output in the console will be −[ 7, 1, 6, 4, 5, 8 ]
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
4K+ Views
Suppose we have two arrays of objects, the first of which contains some objects with user ids and user names.The array contains objects with user ids and user addresses.The array is −const arr1 = [ {"id":"123", "name":"name 1"}, {"id":"456", "name":"name 2"} ]; const arr2 = [ {"id":"123", "address":"address 1"}, {"id":"456", "address":"address 2"} ];We are required to write a JavaScript function that takes in two such arrays and merges these two arrays to form a third array.The third array should contain the user id, name, and address object of corresponding users.ExampleThe code for this will be −const ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
475 Views
We are required to write a JavaScript function that takes in an array of literal values. Our function should pick all those values from the array that appears exactly twice in the array and return a new array of those elements.ExampleThe code for this will be −const arr = [0, 1, 2, 2, 3, 3, 5]; const findAppearances = (arr, num) => { let count = 0; for(let i = 0; i < arr.length; i++){ const el = arr[i]; if(num !== el){ continue; }; ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
408 Views
Suppose, we have two different array of objects that contains information about the questions answered by some people −const arr1=[ { PersonalID: '11', qusetionNumber: '1', value: 'Something' }, { PersonalID: '12', qusetionNumber: '2', value: 'whatever' }, { PersonalID: '13', qusetionNumber: '3', value: 'anything' }, { PersonalID: '14', qusetionNumber: '4', value: 'null' } ]; const arr2=[ { qusetionNumber: '2', chID: '111', cValue: 'red' }, { qusetionNumber: '2', chID: '112', cValue: 'green'}, { qusetionNumber: '2', chID: '113', cValue: 'blue' }, {qusetionNumber: '3', choiceID: '114', cValue: 'yellow'}, {qusetionNumber: '4', choiceID: '115', cValue: 'red'} ];We ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
316 Views
We are required to write a JavaScript function that takes in a multidimensional array of arrays of literal values. Our function should return the intersecting array of all the subarrays present in the input array.ExampleThe code for this will be −const arr = [ ["garden","canons","philips","universal"], ["universal","ola","uber","bangalore"] ]; const findMultiIntersection = (arr = []) => { const res = []; arr.forEach(el => { const thisObj = this; el.forEach(element => { if(!thisObj[element]){ thisObj[element] = true; } else{ res.push(element) }; }); }, {}); return res; }; console.log(findMultiIntersection(arr));OutputAnd the output in the console will be −[ 'universal' ]