Found 9150 Articles for Object Oriented Programming

map() array of object titles into a new array based on other property value JavaScript

AmitDiwan
Updated on 25-Aug-2020 06:44:54

234 Views

Let’s say, we have an array of objects like this −const arr = [{    country: "cananda",    count: 2 }, {       country: "jamaica",       count: 2 }, {       country: "russia",       count: 1 }, {       country: "india",       count: 3 }, {       country: "spain",       count: 2 }, {       country: "portugal",       count: 1 }, {       country: "italy",       count: 1 }];We are required to write a function that ... Read More

JavaScript equivalent of Python's zip function

Alshifa Hasnain
Updated on 14-Jan-2025 03:27:00

1K+ Views

In this article, we will learn the JavaScript equivalent of Python's zip Function In Python, the zip() function is a convenient way to combine multiple tables into a single iterable of tuples. However, JavaScript does not have a built-in equivalent for this functionality Problem Statement Given two or more arrays of equal length, implement a function in JavaScript that combines these arrays into a single variety of arrays, where each inner array contains the elements from the input arrays at the corresponding positions. Input arr1 = [1, 2, 3], arr2 = ['a', 'b', 'c']Output [[1, 'a'], [2, 'b'], [3, 'c']] ... Read More

Construct string via recursion JavaScript

AmitDiwan
Updated on 25-Aug-2020 06:41:33

327 Views

We are required to write a recursive function, say pickString that takes in a string that contains a combination of alphabets and numbers and returns a new string consisting of only alphabets.For example, If the string is ‘dis122344as65t34er’, The output will be: ‘disaster’Therefore, let’s write the code for this recursive function −Exampleconst str = 'ex3454am65p43le'; const pickString = (str, len = 0, res = '') => {    if(len < str.length){       const char = parseInt(str[len], 10) ? '' : str[len];       return pickString(str, len+1, res+char);    };    return res; }; console.log(pickString(str)); console.log(pickString('23123ca43n y43ou54 6do884 ... Read More

Find and return array positions of multiple values JavaScript

AmitDiwan
Updated on 25-Aug-2020 06:40:06

3K+ Views

We have to write a function, say findPositions() that takes in two arrays as argument. And it should return an array of the indices of all the elements of the second array present in the first array.For example −If the first array is [‘john’, ‘doe’, ‘chris’, ‘snow’, ‘john’, ‘chris’], And the second array is [‘john’, chris]Then the output should be −[0, 2, 4, 5]Therefore, let’s write the code for this function. We will use a forEach() loop here;Exampleconst values = ['michael', 'jordan', 'jackson', 'michael', 'usain', 'jackson', 'bolt', 'jackson']; const queries = ['michael', 'jackson', 'bolt']; const findPositions = (first, second) => ... Read More

How can I remove a specific item from an array in JavaScript

AmitDiwan
Updated on 25-Aug-2020 06:38:31

264 Views

We are required to write a function for arrays Array.prototype.remove(). It accepts one argument; it is either a callback function or a possible element of the array. If it’s a function then the return value of that function should be considered as the possible element of the array and we have to find and delete that element from the array in place and the function should return true if the element was found and deleted otherwise it should return false.Therefore, let’s write the code for this function −Exampleconst arr = [12, 45, 78, 54, 1, 89, 67]; const names = ... Read More

Sort the numbers so that the even numbers are ahead JavaScript

AmitDiwan
Updated on 25-Aug-2020 06:36:55

205 Views

We have an array of Numbers that contains some positive and negative even and odd numbers. We are required to sort the array in ascending order but all the even numbers should appear before any of the odd number and all the odd numbers should appear after all the even numbers and obviously both sorted within.Therefore, for example −If the input array is −const arr = [-2, 3, 6, -12, 9, 2, -4, -11, -8];Then the output should be −[ -12, -8, -4, -2, 2, 6, -11, 3, 9]Therefore, let’s write the code for this sort function −Exampleconst arr = ... Read More

Filter the properties of an object based on an array and get the filtered object JavaScript

AmitDiwan
Updated on 25-Aug-2020 06:33:33

458 Views

We have to write a function that takes in an object and a string literals array, and it returns the filtered object with the keys that appeared in the array of strings.For example − If the object is {“a”: [], “b”: [], “c”:[], “d”: []} and the array is [“a”, “d”] then the output should be −{“a”: [], “d”:[]}Therefore, let’s write the code for this function, We will iterate over the keys of the object whether it exists in the array, if it does, if shove that key value pair into a new object, otherwise we keep iterating and return ... Read More

JavaScript function to accept a string and mirrors its alphabet

AmitDiwan
Updated on 25-Aug-2020 06:31:20

313 Views

We have to write a function that accepts a string and mirrors its alphabet. For example −If the input is ‘abcd’ The output should be ‘zyxw’The function simply takes every character and map to the that is (26 - N) alphabets away from it, where is the 1 based index of that alphabet like 5 for e and 10 for j.We will use the String.prototype.replace() method here, to match all the English alphabets irrespective of their case. The full code for this function will be −Exampleconst str = 'ABCD'; const mirrorString = str => {    const regex = /[A-Za-z]/g; ... Read More

In JavaScript, need to perform sum of dynamic array

AmitDiwan
Updated on 25-Aug-2020 06:28:24

326 Views

Let’s say, we have an array that contains the score of some players in different sports. The scores are represented like this −const scores = [    {sport: 'cricket', aman: 54, vishal: 65, jay: 43, hardik: 88, karan:23},    {sport: 'soccer', aman: 14, vishal: 75, jay: 41, hardik: 13, karan:73},    {sport: 'hockey', aman: 43, vishal: 35, jay: 53, hardik: 43, karan:29},    {sport: 'volleyball', aman: 76, vishal: 22, jay: 36, hardik: 24, karan:47},    {sport: 'baseball', aman: 87, vishal: 57, jay: 48, hardik: 69, karan:37}, ];We have to write a function that takes in this array and returns a ... Read More

How to count a depth level of nested JavaScript objects?

AmitDiwan
Updated on 25-Aug-2020 06:25:25

3K+ Views

We have an array of objects, which further have nested objects like this −const arr = [{    id: 0, children: [] }, {       id: 1, children: [{       id: 2, children: [] }, {       id: 3, children: [{          id: 4, children: []       }]    }] }];Our job is to write a recursive function, say assignDepth() that takes in this array and assigns depth property to each nested object. Like the object with id 0 will have depth 0, id 1 will have depth 1 ... Read More

Advertisements