Found 9316 Articles for Object Oriented Programming

Combine two different arrays in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:32:20

302 Views

Suppose we have two arrays, the first array contains the scheduled date for some events and the second array contains the names of those events, like this −const dates = [    {       id:"1",       date:"2017-11-07"    },    {       id:"1",       date:"2017-11-08"    },    {       id:"2",       date:"2017-11-07"    },    {       id:"2",       date:"2017-11-08"    } ]; const names = [    {       id:"1",       name:"Pervies, Peter"    },    {   ... Read More

Finding number of occurrences of the element appearing most number of times in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:26:58

349 Views

We are required to write a JavaScript function that takes in an array of literals and returns the count of elements that appears for the most number of times in the array.ExampleThe code for this will be −let arr = [2, 8, 4, 8, 6, 4, 7, 8]; const countOccurence = arr => {    const max = arr.reduce((acc, val) => {       return Math.max(acc, val);    }, -Infinity);    const count = arr.filter(el => {       return el === max;    });    const { length } = count;    return length; }; console.log(countOccurence(arr));OutputThe output in the console −3

How to filter out common array in array of arrays in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:25:35

190 Views

Suppose we have an array of arrays like this −const arr = [    [       "Serta",       "Black Friday"    ],    [       "Serta",       "Black Friday"    ],    [       "Simmons",       "Black Friday"    ],    [       "Simmons",       "Black Friday"    ],    [       "Simmons",       "Black Friday"    ],    [       "Simmons",       "Black Friday"    ] ];We are required to write a JavaScript function that ... Read More

Sort an array according to another array in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:22:53

258 Views

Suppose we have two arrays of literals like these −const arr1 = [1, 3, 2, 4, 5, 6]; const arr2 = [1, 2, 5];We are required to write a JavaScript function that takes in two such arrays. Then our function should return a new array that contains all the elements of arr1 but sorted according to arr2.Like the elements that appear in both the array should appear first according to their order in the second array followed by the elements only present in first array retaining their order.ExampleThe code for this will be −const arr1 = [1, 3, 2, 4, ... Read More

Removing duplicate objects from array in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:20:43

218 Views

Suppose, we have an array of objects like this −const arr = [    {"title": "Assistant"},    {"month": "July"},    {"event": "Holiday"},    {"title": "Assistant"} ];We are required to write a JavaScript function that takes in one such array. Our function should then return a new array that contains all the object from the original array but the duplicate ones.ExampleThe code for this will be −const arr = [    {"title": "Assistant"},    {"month": "July"},    {"event": "Holiday"},    {"title": "Assistant"} ]; const removeDuplicate = arr => {    const map = {};    for(let i = 0; i < ... Read More

How to merge two strings alternatively in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:17:20

926 Views

We are required to write a JavaScript function that takes in two. Our function should then return a new array that contains characters alternatively from both the strings.For example: If the two strings are −const str1 = 'abc'; const str2 = 'def';OutputThen the output should be −const output = 'adbecf';ExampleThe code for this will be −const str1 = 'abc'; const str2 = 'def'; const mergeAlternatively = (str1, str2) => {    const a = str1.split("").filter(el => !!el);    const b = str2.split("");    let mergedString = '';    for(var i = 0; i < a.length || i < b.length; i++){ ... Read More

Removing duplicate values in a twodimensional array in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:15:55

1K+ Views

We are required to write a JavaScript function that takes in a two-dimensional array of literals.Our function should return a new array that contains all the entries from the original array but the repeating ones.ExampleThe code for this will be −const arr = [    [1,2,3,4,5],    [3,4,6,7,8,2],    [7,2,4,9,11,15],    [10,12,3,7,11] ]; const removeDuplicates = arr => {    let map = {};    let res = [];    res = arr.map(el => {       return el.filter(val => {          if(map[val]){             return false;          };          map[val] = 1;          return true;       });    });    return res; }; console.log(removeDuplicates(arr));OutputThe output in the console −[ [ 1, 2, 3, 4, 5 ], [ 6, 7, 8 ], [ 9, 11, 15 ], [ 10, 12 ] ]

Retaining array elements greater than cumulative sum using reduce() in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:13:57

85 Views

We are required to write a JavaScript function that takes in an array of Numbers. Our function should return a new array that contains all the elements from the original array that are greater than the cumulative sum of all elements up to that point. We are required to solve this problem using the Array.prototype.reduce() function.ExampleLet’s write the code for this function −const arr = [1, 2, 30, 4, 5, 6]; const retainGreaterElements = arr => {    let res = [];    arr.reduce((acc, val) => {       return (val > acc && res.push(val), acc + val);    }, 0);    return res; } console.log(retainGreaterElements(arr));OutputThe output in the console −[1, 2, 30]

Cumulative sum of elements in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:12:19

1K+ Views

Suppose, we have an array of numbers like this −const arr = [1, 2, 3, 4, 5, 6];We are required to write a JavaScript function that takes in one such array and returns a new array with corresponding elements of the array being the sum of all the elements upto that point from the original array.Therefore, for the above array, the output should be −const output = [1, 3, 6, 10, 15, 21];ExampleThe code for this will be −const arr = [1, 2, 3, 4, 5, 6]; const findCumulativeSum = arr => {    const creds = arr.reduce((acc, val) => ... Read More

Replace all occurrence of specific words in a sentence based on an array of words in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:10:32

418 Views

We are required to write a JavaScript function that takes a string and an array of strings.Our function should return a new string, where all the occurrences of the word in the string that are present in the array are replaced by a whitespace.Our function should use the String.prototype.replace() method to solve this problem.ExampleThe code for this will be −var excludeWords = ["A", "ABOUT", "ABOVE", "ACROSS", "ALL", "ALONG", "AM", "AN", "AND", "ANY", "ASK", "AT", "AWAY", "CAN", "DID", "DIDN'T", "DO", "DON'T", "FOR", "FROM", "HAD", "HAS", "HER", "HIS", "IN", "INTO", "IS", "IT", "NONE", "NOT", "OF", "ON", "One", "OUT", "SO", "SOME", "THAT", "THE", ... Read More

Advertisements