Object Oriented Programming Articles

Page 385 of 588

Search and update array based on key JavaScript

AmitDiwan
AmitDiwan
Updated on 31-Aug-2020 228 Views

We have two arrays like these −let arr1 = [{"LEVEL":4, "POSITION":"RGM"}, {"LEVEL":5, "POSITION":"GM"}, {"LEVEL":5, "POSITION":"GMH"}] let arr2 = [{"EMAIL":"test1@stc.com", "POSITION":"GM"}, {"EMAIL":"test2@stc.com", "POSITION":"GMH"}, {"EMAIL":"test3@stc.com", "POSITION":"RGM"}, {"EMAIL":"test3@CSR.COM.AU", "POSITION":"GM"} ]We are required to write a function that adds the property level to each object of arr2, picking it up from the object from arr1 that have the same value for property "POSITION"Let's write the code for this function −Examplelet arr1 = [{"LEVEL":4, "POSITION":"RGM"}, {"LEVEL":5, "POSITION":"GM"}, {"LEVEL":5, "POSI TION":"GMH"}]    let arr2 = [{"EMAIL":"test1@stc.com", "POSITION":"GM"},    {"EMAIL":"test2@stc.com", "POSITION":"GMH"},    {"EMAIL":"test3@stc.com", "POSITION":"RGM"},    {"EMAIL":"test3@CSR.COM.AU", "POSITION":"GM"} ] const formatArray = (first, second) => {    second.forEach((el, ...

Read More

How to split last n digits of each value in the array with JavaScript?

AmitDiwan
AmitDiwan
Updated on 31-Aug-2020 357 Views

We have an array of literals like this −const arr = ["", 20191219, 20191220, 20191221, 20191222, 20191223, 20191224, 20191225];We are required to write a JavaScript function that takes in this array and a number n and if the corresponding element contains more than or equal to n characters, then the new element should contain only the last n characters otherwise the element should be left as it is.Let's write the code for this function −Exampleconst arr = ["", 20191219, 20191220, 20191221, 20191222, 20191223, 20191224, 20191225]; const splitElement = (arr, num) => {    return arr.map(el => {       if(String(el).length

Read More

JavaScript Return an array that contains all the strings appearing in all the subarrays

AmitDiwan
AmitDiwan
Updated on 31-Aug-2020 329 Views

We have an array of arrays like this −const arr = [    ['foo', 'bar', 'hey', 'oi'],    ['foo', 'bar', 'hey'],    ['foo', 'bar', 'anything'],    ['bar', 'anything'] ]We are required to write a JavaScript function that takes in such array and returns an array that contains all the strings which appears in all the subarrays.Let's write the code for this functionExampleconst arr = [    ['foo', 'bar', 'hey', 'oi'],    ['foo', 'bar', 'hey'],    ['foo', 'bar', 'anything'],    ['bar', 'anything'] ] const commonArray = arr => {    return arr.reduce((acc, val, index) => {       return acc.filter(el => val.indexOf(el) !== -1);    }); }; console.log(commonArray(arr));OutputThe output in the console will be −['bar']

Read More

Separate odd and even in JavaScript

AmitDiwan
AmitDiwan
Updated on 31-Aug-2020 810 Views

We are required to write a JavaScript function that takes in an array of numbers and returns an array with all even numbers appearing on the left side of any odd number and all the odd numbers appearing on the right side of any even number.Therefore, let's write the code for this function −Exampleconst arr = [2, 6, 3, 7, 8, 3, 5, 4, 3, 6, 87, 23, 2, 23, 67, 4]; const isEven = num => num % 2 === 0; const sorter = (a, b) => {    if(isEven(a) && !isEven(b)){       return -1;    };    if(!isEven(a) && isEven(b)){       return 1;    };    return 0; }; arr.sort(sorter); console.log(arr);OutputThe output in the console will be −[    2, 6, 8, 4, 6, 2,    4, 3, 7, 3, 5, 3,    87, 23, 23, 67 ]

Read More

Wildcard matching of string JavaScript

AmitDiwan
AmitDiwan
Updated on 31-Aug-2020 2K+ Views

We are required to write a JavaScript function that accepts two strings and a number n. The function matches the two strings i.e., it checks if the two strings contains the same characters. The function should return true if both the strings contain the same character irrespective of their order or if they contain at most n different characters, otherwise the function should return false.Let's write the code for this function −Exampleconst str1 = 'first string'; const str2 = 'second string'; const wildcardMatching = (first, second, num) => {    let count = 0;    for(let i = 0; i ...

Read More

Object difference in JavaScript

AmitDiwan
AmitDiwan
Updated on 31-Aug-2020 170 Views

We are required to write a JavaScript function that takes in two objects (possibly nested) and returns a new object with key value pair for the keys that were present in the first object but not in the secondLet's write the code for this function −Exampleconst obj1 = {    "firstName": "Raghav",    "lastName": "Raj",    "age": 43,    "address": "G-12 Kalkaji",    "email": "raghavraj1299@yahoo.com",    "salary": 90000 }; const obj2 = {    "lastName": "Raj",    "address": "G-12 Kalkaji",    "email": "raghavraj1299@yahoo.com",    "salary": 90000 }; const objectDifference = (first, second) => {    return Object.keys(first).reduce((acc, val) => { ...

Read More

Rearrange string so that same character become n distance apart JavaScript

AmitDiwan
AmitDiwan
Updated on 31-Aug-2020 317 Views

We are required to write a JavaScript function that takes in a string with repetitive characters and returns a new string in which all the same characters are exactly n characters away from each other. And the number should be smaller than the length of the array.For example −If the input string is: "accessories" And the number n is 3 Then, The return value should be: "secrsecisao"Note − There may be some other permutation to achieve the required output, the order is not important, we should stick to the logic and as long as we fulfil it our output is ...

Read More

JavaScript R- eturn Array Item(s) With Largest Score

AmitDiwan
AmitDiwan
Updated on 31-Aug-2020 185 Views

We have an array of arrays that contains the marks scored by some students in some subjects −const arr = [    ['Math', 'John', 100],    ['Math', 'Jake', 89],    ['Math', 'Amy', 93],    ['Science', 'Jake', 89],    ['Science', 'John', 89],    ['Science', 'Amy', 83],    ['English', 'John', 82],    ['English', 'Amy', 81],    ['English', 'Jake', 72] ];We are required to write a function that takes in this array and retuns an array of object, with one object for each subject and the details about the top scorer of that subject.Our output should look like −[    { "Subject": "Math", ...

Read More

Split string into equal parts JavaScript

AmitDiwan
AmitDiwan
Updated on 31-Aug-2020 1K+ Views

We are required to write a JavaScript function that takes in a string and a number n as two arguments (the number should be such that it exactly divides the length of string). And we have to return an array of n strings of equal length.For example −If the string is "helloo" and the number is 3 Our output should be: ["ho", "eo", "ll"]Here, each substring exactly contains (length of array/n) characters. And each substring is formed by taking corresponding first and last letters of the string alternativelyLet's write the code for this function −Exampleconst str = 'helloo'; const splitEqual ...

Read More

Check for Subarray in the original array with 0 sum JavaScript

AmitDiwan
AmitDiwan
Updated on 31-Aug-2020 483 Views

We are required to write a JavaScript function that takes in an array of Numbers with some positive and negative values. We are required to determine whether there exists a subarray in the original array whose net sum is 0 or not.Our function should return a boolean on this basis.ApproachThe approach here is simple. We iterate over the array using a for loop, calculate the cumulative sum up to that particular element. And if any point the cumulative becomes 0 or attains a value it has previously attained, then there exists a subarray with sum 0. Otherwise there exists no ...

Read More
Showing 3841–3850 of 5,877 articles
« Prev 1 383 384 385 386 387 588 Next »
Advertisements