Found 9313 Articles for Object Oriented Programming

How do I write a function that takes an array of values and returns an object JavaScript?

AmitDiwan
Updated on 26-Aug-2020 09:41:04

495 Views

Let’s say, we are required to write a function classifyArray() that takes in an array which contains mixed data types and returns a Map() with the elements grouped by their data types.For example −// if the input array is: const arr = ['class', 2, [7, 8, 9], {"name": "Michael"}, Symbol('foo'), true, false, 'name', 6]; // then the output Map should be: Map(5) {    'string' => [ 'class', 'name' ],    'number' => [ 2, 6 ],    'object' => [ [ 7, 8, 9 ], { name: 'Michael' } ],    'symbol' => [ Symbol(foo) ],    'boolean' => ... Read More

Recursively flat an object JavaScript

AmitDiwan
Updated on 26-Aug-2020 09:38:41

279 Views

We are required to write a function that does the following transformation −If the input object is −const input = {    a: 0,    b: {x: {y: 1, z: 2}},    c: 3 };Then the output of the function should be −const output = {    a: 0,    'b.x.y': 1,    'b.x.z': 2,    c: 3 }We basically have to write a function that flattens a nested object. Will do this via recursion and the code for doing this will be −Exampleconst obj = {    a: 1,    b: {x: {y: 1, z: 2}},    c: 3 ... Read More

Parse array to equal intervals in JavaScript

AmitDiwan
Updated on 26-Aug-2020 09:36:25

140 Views

Let’s say, we are required to write a function, say parseEqualInterval() that takes in an array of Numbers of strictly two elements as the first argument and a number n as the second argument and it inserts n-1 equidistant entries between the actual two elements of the original array so that it gets divided into n equal intervals.For example −// if the input array is const arr = [12, 48]; // and the interval is 4 //then the output array should be: const output = [12, 21, 30, 39, 48];This way the array got divided into 4 equal intervals. So, ... Read More

How to check if every property on object is the same recursively in JavaScript?

AmitDiwan
Updated on 26-Aug-2020 09:34:21

327 Views

Let’s say, we are required to write a function, say isSame() that accepts a nested object and returns a boolean depending on the fact whether or not all the keys have the same values. When saying all the keys we mean all the last keys like if a key has a nested object as its value, we are required to traverse to the end of the nested object and check for that value.For example − If the object is −const obj = {    a: 1,    b: 1,    c: {       aa: 1    } };Then ... Read More

Rearrange an array in maximum minimum form by JavaScript

AmitDiwan
Updated on 26-Aug-2020 09:31:44

207 Views

We are required to write a function, say minMax() that takes in an array of Numbers and rearranges the elements such that the greatest element appears first followed by the smallest elements then the second greatest element followed by second smallest element and so on.For example −// if the input array is: const input = [1, 2, 3, 4, 5, 6, 7] // then the output should be: const output = [7, 1, 6, 2, 5, 3, 4]So, let’s write the complete code for this function −Exampleconst input = [1, 2, 3, 4, 5, 6, 7]; const minMax = arr ... Read More

How to count the number of elements in an array below/above a given number (JavaScript)

AmitDiwan
Updated on 26-Aug-2020 09:30:08

132 Views

Consider we have an array of Numbers that looks like this −const array = [3.1, 1, 2.2, 5.1, 6, 7.3, 2.1, 9];We are required to write a function that counts how many of the elements are in the array below / above a given number.For example, if the number is 5.25, the answer should be the following 5 elements, (3.1, 1, 2.2, 5.1, 2.1)and 3 elements above it −(6, 7.3, 9)Note − If any element is equal to the provided number, it should be counted as above the number.So, let’s write the code for this function −Exampleconst array = [3.1, ... Read More

Group values in array by two properties JavaScript

AmitDiwan
Updated on 25-Aug-2020 07:56:05

845 Views

We have an array of objects like this −const arr = [    { value: 12, gap: 1 },    { value: 13, gap: 1 },    { value: 14, gap: 1 },    { value: 15, gap: 1 },    { value: 19, gap: 2 },    { value: 21, gap: 1 },    { value: 22, gap: 1 },    { value: 23, gap: 1 },    { value: 27, gap: 1 },    { value: 31, gap: 4 },    { value: 35, gap: 4 },    { value: 39, gap: 4 },    { value: 43, ... Read More

Form Object from string in JavaScript

AmitDiwan
Updated on 25-Aug-2020 07:53:01

217 Views

We are required to write a function that takes in a string as the first and the only argument and constructs an object with its keys based on the unique characters of the string and value of each key being defaulted to 0.For example −// if the input string is: const str = 'hello world!'; // then the output should be: const obj = {"h": 0, "e": 0, "l": 0, "o": 0, " ": 0, "w": 0, "r": 0, "d": 0, "!": 0};So, let’s write the code for this function −Exampleconst str = 'hello world!'; const stringToObject = str => ... Read More

Group array by equal values JavaScript

AmitDiwan
Updated on 25-Aug-2020 07:43:26

876 Views

Let’s say, we have an array of string / number literals that contains some duplicate values like this −const array = ['day', 'night', 'afternoon', 'night', 'noon', 'night', 'noon', 'day', 'afternoon', 'day', 'night'];We are required to write a function groupSimilar() that takes in this array and returns a new array where all the repeating entries are group together in a subarray as the first element and their total count in the original array as the second element.So, for this example, the output should be −[    [ 'day', 3 ],    [ 'night', 4 ],    [ 'afternoon', 2 ],   ... Read More

Changing an array in place using splice() JavaScript

AmitDiwan
Updated on 25-Aug-2020 07:41:48

110 Views

We are required to write a function that, given an array arr and a number n, returns the array with elements repeating no more than n times. And we have to do all this without disturbing the indices of desired elements. So, let’s write the code for this function, We will keep the count of all the elements in a hashmap and during iteration whenever the count of any element exceeds the maximum count we will splice that element. The code for this will be −Exampleconst arr = [7, 26, 21, 41, 43, 2, 26, 24, 10, 26, 10, 10, ... Read More

Advertisements