Found 9313 Articles for Object Oriented Programming

Compare two arrays and get those values that did not match JavaScript

AmitDiwan
Updated on 28-Aug-2020 13:34:47

4K+ Views

We have two arrays of literals that contain some common values, our job is to write a function that returns an array with all those elements from both arrays that are not common.For example −// if the two arrays are: const first = ['cat', 'dog', 'mouse']; const second = ['zebra', 'tiger', 'dog', 'mouse']; // then the output should be: const output = ['cat', 'zebra', 'tiger'] // because these three are the only elements that are not common to both arraysLet’s write the code for this −We will spread the two arrays and filter the resulting array to obtain an array ... Read More

How to generate child keys by parent keys in array JavaScript?

AmitDiwan
Updated on 26-Aug-2020 11:51:45

2K+ Views

Let’s say, we have an array of objects like this −const arr = [    { id: 1, parent_id: 0, title: 'Movies' },    { id: 2, parent_id: 0, title: 'Music' },    { id: 3, parent_id: 1, title: 'Russian movies' },    { id: 4, parent_id: 2, title: 'Russian music' },    { id: 5, parent_id: 3, title: 'New' },    { id: 6, parent_id: 3, title: 'Top10' },    { id: 7, parent_id: 4, title: 'New' },    { id: 8, parent_id: 4, title: 'Top10' },    { id: 9, parent_id: 0, title: 'Soft' } ];We are required ... Read More

Ordering string in an array according to a number in the string JavaScript

AmitDiwan
Updated on 26-Aug-2020 11:49:02

65 Views

We have an array of strings each of which contain one or more numbers like this −const arr = ['di5aster', 'ca1amity', 'cod3', 'ho2me3', 'ca11ing'];We are required to write a sorting function that sorts this array in ascending order of the numbers present in the strings. The correct order will be −const output = [ 'ca1amity', 'cod3', 'di5aster', 'ca11ing', 'ho2me3' ];Therefore, let’s write the code for this problem −Exampleconst arr = ['di5aster', 'ca1amity', 'cod3', 'ho2me3', 'ca11ing']; const filterNumber = str => {    return +str    .split("")    .filter(el => el.charCodeAt() >= 48 && el.charCodeAt() {    return filterNumber(a) - ... Read More

How to convert an array into a complex array JavaScript?

AmitDiwan
Updated on 26-Aug-2020 11:47:16

226 Views

Let’s say, we are required to write a function that takes in an array of Numbers and number n, where n >= any number of the array. The function is required to break the array into subarrays if the sum of consecutive elements of the array exceeds the number n.For example −// if the original array is: const arr = [2, 1, 2, 1, 1, 1, 1, 1]; // and the number n is 4 // then the output array should be: const output = [ [ 2, 1 ], [ 2, 1, 1 ], [ 1, 1, 1 ] ... Read More

Remove duplicates from an array keeping its length same in JavaScript

AmitDiwan
Updated on 26-Aug-2020 11:44:56

132 Views

We have to write a function that takes in an array, removes all duplicates from it and inserts the same number of empty strings at the end.For example − If we find 4 duplicate values we have to remove then all and insert four empty strings at the end.Therefore, let’s write the code for this problem −Exampleconst arr = [1, 2, 3, 1, 2, 3, 2, 2, 3, 4, 5, 5, 12, 1, 23, 4, 1]; const deleteAndInsert = arr => {    const creds = arr.reduce((acc, val, ind, array) => {       let { count, res } ... Read More

Finding a greatest number in a nested array in JavaScript

AmitDiwan
Updated on 26-Aug-2020 11:43:04

174 Views

We have to write a simple function in JavaScript that takes in an array of Numbers (nested to any level) and return the greatest number present in the array.For example − If the input array is −const arr = [    34, 65, 67,    [       43, 76, 87, 23, 56, 7,       [          54, 7, 87, 23, 79, 314, 2       ],       54    ], 54, 4, 2 ];Then the output should be −314We will use recursion to find the greatest number in the array. ... Read More

Getting elements of an array depending on corresponding values of another JavaScript

AmitDiwan
Updated on 26-Aug-2020 11:41:41

482 Views

Suppose we have two arrays, for an example consider the following two −const array1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']; const array2 = [ 1, 0, 0, 1 , 0, 0, 1, 0];Both the arrays are bound to have the same length. We are required to write a function that when provided with an element from the second array, returns a subarray from the first array of the all elements whose index corresponds to the index of the element we took as an argument in the second array.For example: findSubArray(0) should return −[‘b’, ‘c’, ‘e’, ‘f’, ‘h’]Because ... Read More

Top n max value from array of object JavaScript

AmitDiwan
Updated on 26-Aug-2020 11:39:52

1K+ Views

Let’s say, we have an array of objects like this −const arr = [    {"id":0, "start":0, "duration":117, "slide":4, "view":0},    {"id":0, "start":0, "duration":12, "slide":1, "view":0},    {"id":0, "start":0, "duration":41, "slide":2, "view":0},    {"id":0, "start":0, "duration":29, "slide":3, "view":0},    {"id":0, "start":0, "duration":123, "slide":3, "view":0},    {"id":0, "start":0, "duration":417, "slide":2, "view":0},    {"id":0, "start":0, "duration":12, "slide":1, "view":0},    {"id":0, "start":0, "duration":67, "slide":2, "view":0} ];We have to write a function that takes in this array and returns the top n element of the array in another array (top means the object that has the highest value for duration).Therefore, let’s write the code ... Read More

Count unique elements in array without sorting JavaScript

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

250 Views

Let’s say, we have an array of literals that contains some duplicate values −const arr = ['Cat', 'Dog', 'Cat', 'Elephant', 'Dog', 'Grapes', 'Dog', 'Lion', 'Grapes', 'Lion'];We are required to write a function that returns the count of unique elements in the array. Will use Array.prototype.reduce() and Array.prototype.lastIndexOf() to do this −Exampleconst arr = ['Cat', 'Dog', 'Cat', 'Elephant', 'Dog', 'Grapes', 'Dog', 'Lion', 'Grapes', 'Lion']; const countUnique = arr => {    return arr.reduce((acc, val, ind, array) => {       if(array.lastIndexOf(val) === ind){          return ++acc;       };       return acc;    }, 0); }; console.log(countUnique(arr));OutputThe output in the console will be −5

Reorder array based on condition in JavaScript?

AmitDiwan
Updated on 26-Aug-2020 11:35:08

2K+ Views

Let’s say we have an array of object that contains the scores of some players in a card game −const scorecard = [{    name: "Zahir",    score: 23 }, {       name: "Kabir",       score: 13 }, {       name: "Kunal",       score: 29 }, {       name: "Arnav",       score: 42 }, {       name: "Harman",       score: 19 }, {       name: "Rohit",       score: 41 }, {       name: "Rajan",       score: ... Read More

Advertisements