- 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 9314 Articles for Object Oriented Programming
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
251 Views
Suppose, we have an array of objects like this −const arr = [ {id: 1, name: "Mohan"}, {id: 2, name: "Sohan"}, {id: 3, name: "Rohan"} ];We are required to write a function that takes one such array and constructs an object from it with the id property as key and name as valueThe output for the above array should be −const output = {1:{name:"Mohan"}, 2:{name:"Sohan"}, 3:{name:"Rohan"}}ExampleFollowing is the code −const arr = [ {id: 1, name: "Mohan"}, {id: 2, name: "Sohan"}, {id: 3, name: "Rohan"} ]; const arrayToObject = arr => { const ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
84 Views
Suppose, we have an array of objects like this −const arr = [ {'ID-01':1}, {'ID-02':3}, {'ID-01':3}, {'ID-02':5} ];We are required to add the values for all these objects together that have identical keysTherefore, for this array, the output should be −const output = [{'ID-01':4}, {'ID-02':8}];We will loop over the array, check for existing objects with the same keys, if they are there, we add value to it otherwise we push new objects to the array.ExampleFollowing is the code −const arr = [ {'ID-01':1}, {'ID-02':3}, {'ID-01':3}, {'ID-02':5} ]; const indexOf = function(key){ ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
261 Views
Suppose we have an array like this −const arr = [ {unit: 35, brand: 'CENTURY'}, {unit: 35, brand: 'BADGER'}, {unit: 25, brand: 'CENTURY'}, {unit: 15, brand: 'CENTURY'}, {unit: 25, brand: 'XEGAR'} ];We are required to write a function that groups all the brand properties of objects whose unit property is the same.Like for the above array, the new array should be −const output = [ {unit: 35, brand: 'CENTURY, BADGER'}, {unit: 25, brand: 'CENTURY, XEGAR'}, {unit: 15, brand: 'CENTURY'} ];We will loop over the array, search for the object with unit value ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
839 Views
We are required to write a JavaScript function that takes in an array of String / Number literals and returns a subarray of all the elements that were palindrome in the original array.For example −If the input array is −const arr = ['carecar', 1344, 12321, 'did', 'cannot'];Then the output should be −const output = [12321, 'did'];We will create a helper function that takes in a number or a string and checks if it’s a boolean or not. Then we will loop over the array, filter the palindrome elements and return the filtered arrayExampleFollowing is the code −const arr = ['carecar', ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
135 Views
Suppose, we have an object like this −const products = { "Pineapple":38, "Apple":110, "Pear":109 };All the keys are unique in themselves and all the values are unique in themselves. We are required to write a function that accepts a value and returns its keyFor example: findKey(110) should return −"Apple"We will approach this problem by first reverse mapping the values to keys and then simply using object notations to find their values.ExampleFollowing is the code −const products = { "Pineapple":38, "Apple":110, "Pear":109 }; const findKey = (obj, val) => { const res = {}; ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
451 Views
We are required to write a JavaScript function that takes in a string and any number of characters specified as separators. Our function should return a splitted array of the string based on all the separators specified.For example −If the string is −const str = 'rttt.trt/trfd/trtr, tr';And the separators are −const sep = ['/', '.', ', '];Then the output should be −const output = [ 'rttt', 'trt', 'trfd', 'trtr' ];ExampleFollowing is the code −const str = 'rttt.trt/trfd/trtr, tr'; const splitMultiple = (str, ...separator) => { const res = []; let start = 0; for(let i = 0; ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
1K+ Views
We are required to write a function that, given a number, say, 123, will output an array −[100,20,3]Basically, the function is expected to return an array that contains the place value of all the digits present in the number taken as an argument by the function.We can solve this problem by using a recursive approach.ExampleFollowing is the code −const num = 123; const placeValue = (num, res = [], factor = 1) => { if(num){ const val = (num % 10) * factor; res.unshift(val); return placeValue(Math.floor(num / 10), res, factor * 10); }; return res; }; console.log(placeValue(num));OutputThis will produce the following output in console −[ 100, 20, 3 ]
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
302 Views
Suppose we have an array of arrays of numbers like this −const arr = [[1, 45], [1, 34], [1, 49], [2, 34], [4, 78], [2, 67], [4, 65]];Each subarray is bound to contain strictly two elements. We are required to write a function that constructs a new array where all second elements of the subarrays that have similar first value are grouped together.Therefore, for the array above, the output should look like −const output = [ [45, 34, 49], [34, 67], [78, 65] ];We can make use of the Array.prototype.reduce() method that takes help of a Map() ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
551 Views
We are required to write a JavaScript function that creates a multi-dimensional array based on some inputs. It should take in three elements, namely −row − the number of subarrays to be present in the array, col − the number of elements in each subarrayval minus; the val of each element in the subarraysFor example, if the three inputs are 2, 3, 10Then the output should be −const output = [[10, 10, 10], [10, 10, 10]];ExampleFollowing is the code −const row = 2; const col = 3; const val = 10; const constructArray = (row, col, val) => { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
1K+ Views
Suppose we have an array of two elements with both of its elements being two asynchronous functions. We are required to do some work, say print something to the console (for the purpose of this question) when the execution of both the async function completes.How can we approach this challenge?There are basically two ways to perform some task upon completion of some asynchronous task −Using promisesUsing async/await functionsBut when the code includes dealing with many (more than one) asynchronous functions then the Promise.all function of the former has an edge over the latter.ExampleFollowing is the code −const arr = [ ... Read More