Read by Key and Parse as JSON in JavaScript

AmitDiwan
Updated on 20-Nov-2020 09:52:57

281 Views

Suppose, we have a JSON array like this −const arr = [{    "data": [       { "W": 1, "A1": "123" },       { "W": 1, "A1": "456" },       { "W": 2, "A1": "4578" },       { "W": 2, "A1": "2423" },       { "W": 2, "A1": "2432" },       { "W": 2, "A1": "24324" }    ] }];We are required to write a JavaScript function that takes in one such array and converts it to the following JSON array −[    {       "1": ... Read More

Find Smallest Sum of Indices of Unique Number Pairs in JavaScript

AmitDiwan
Updated on 20-Nov-2020 09:50:29

184 Views

We are required to write a function that takes in an array of numbers as the first argument and a target sum as the second argument. We then want to loop through the array and then add each value to each other (except itself + itself).And if the sum of the two values that were looped through equals the target sum, and the pair of values hasn't been encountered before, then we remember their indices and, at the end, return the full sum of all remembered indices.If the array is −const arr = [1, 4, 2, 3, 0, 5];And the ... Read More

Filtering Array of Objects in JavaScript

AmitDiwan
Updated on 20-Nov-2020 09:48:18

827 Views

Suppose, we have two arrays of literals and objects respectively −const source = [1, 2, 3 , 4 , 5]; const cities = [{ city: 4 }, { city: 6 }, { city: 8 }];We are required to write a JavaScript function that takes in these two arrays. Our function should create a new array that contains all those elements from the array of objects whose value for "city" key is present in the array of literals.ExampleLet us write the code −const source = [1, 2, 3 , 4 , 5]; const cities = [{ city: 4 }, { city: ... Read More

Compare Objects in JavaScript and Return Common Keys with Values

AmitDiwan
Updated on 20-Nov-2020 09:47:10

790 Views

We are required to write a JavaScript function that takes in two objects. The function should return an array of all those common keys that have common values across both objects.ExampleThe code for this will be −const obj1 = { a: true, b: false, c: "foo" }; const obj2 = { a: false, b: false, c: "foo" }; const compareObjects = (obj1 = {}, obj2 = {}) => {    const common = Object.keys(obj1).filter(key => {       if(obj1[key] === obj2[key] && obj2.hasOwnProperty(key)){          return true;       };       return false;    });    return common; }; console.log(compareObjects(obj1, obj2));OutputAnd the output in the console will be −['b', 'c']

Convert Square Bracket Object Keys into Nested Object in JavaScript

AmitDiwan
Updated on 20-Nov-2020 09:44:23

800 Views

We know that there are two ways we can access nested keys within an Object in JavaScript.For instance, take this object −const obj = {    object: {       foo: {          bar: {             ya: 100          }       }    } };If we needed to access or update the nested property 'ya', we can access it like −Way 1 −obj['object']['foo']['bar']['ya']orWay 2 −obj.object.foo.bar.yaBoth these ways lead us to the same destination.We are required to write a JavaScript function that takes in the path to ... Read More

Iterate an Array of Objects and Build a New One in JavaScript

AmitDiwan
Updated on 20-Nov-2020 09:42:41

142 Views

Suppose, we have an array of objects like this −const arr = [    {       "customer": "Customer 1",       "project": "1"    },    {       "customer": "Customer 2",       "project": "2"    },    {       "customer": "Customer 2",       "project": "3"    } ]We are required to write a JavaScript function that takes one such array, and yields (returns) a new array.In the new array, all the customer keys with same values should be merged and the output should look something like this −const output ... Read More

Number of Vowels within an Array in JavaScript

AmitDiwan
Updated on 20-Nov-2020 09:40:22

766 Views

We are required to write a JavaScript function that takes in an array of strings, (they may be a single character or greater than that). Our function should simply count all the vowels contained in the array.ExampleLet us write the code −const arr = ['Amy','Dolly','Jason','Madison','Patricia']; const countVowels = (arr = []) => {    const legend = 'aeiou';    const isVowel = c => legend.includes(c);    let count = 0;    arr.forEach(el => {       for(let i = 0; i < el.length; i++){          if(isVowel(el[i])){             count++;          };       };    });    return count; }; console.log(countVowels(arr));OutputAnd the output in the console will be −10

Array Filtering Using First String Letter in JavaScript

AmitDiwan
Updated on 20-Nov-2020 09:39:08

2K+ Views

Suppose we have an array that contains name of some people like this:const arr = ['Amy', 'Dolly', 'Jason', 'Madison', 'Patricia'];We are required to write a JavaScript function that takes in one such string as the first argument, and two lowercase alphabet characters as second and third argument. Then, our function should filter the array to contain only those elements that start with the alphabets that fall within the range specified by the second and third argument.Therefore, if the second and third argument are 'a' and 'j' respectively, then the output should be −const output = ['Amy', 'Dolly', 'Jason'];ExampleLet us write ... Read More

Sum Ranges Within Another Range in JavaScript

AmitDiwan
Updated on 20-Nov-2020 09:38:04

149 Views

We have two sets of ranges; one is a single range of any length (R1) and the other is a set of ranges (R2) some or parts of which may or may not lie within the single range (R1).We need to calculate the sum of the ranges in (R2) - whole or partial - that lie within the single range (R1).const R1 = [20, 40]; const R2 = [[14, 22], [24, 27], [31, 35], [38, 56]];Result = 2+3+4+2 = 11R1 = [120, 356]; R2 = [[234, 567]];Result 122ExampleLet us write the code −const R1 = [20, 40]; const R2 = [[14, 22], ... Read More

Split Array into Groups in JavaScript

AmitDiwan
Updated on 20-Nov-2020 09:37:38

2K+ Views

We are required to write a JavaScript function that takes in an array of literals and a number and splits the array (first argument) into groups each of length n (second argument) and returns the two-dimensional array thus formed.If the array and number is −const arr = ['a', 'b', 'c', 'd']; const n = 2;Then the output should be −const output = [['a', 'b'], ['c', 'd']];ExampleLet us now write the code −const arr = ['a', 'b', 'c', 'd']; const n = 2; const chunk = (arr, size) => {    const res = [];    for(let i = 0; i ... Read More

Advertisements