Found 8591 Articles for Front End Technology

Remove array duplicates by property - JavaScript

AmitDiwan
Updated on 30-Sep-2020 13:23:13

217 Views

Suppose, we have an array of objects like this −const arr = [{name: "Jack", age: "14"}, {name: "bob", age: "14"}, {name: "sue", age: "21"}, {name: "Jill", age: "16"}, {name: "Jack", age: "21"}];We are required to write a JavaScript function that takes in one such array and removes all the objects that have duplicate values for the name.Therefore, for the above array, the output should be −const arr = [{name: "Jack", age: "14"}, {name: "bob", age: "14"}, {name: "sue", age: "21"}, {name: "Jill", age: "16"}];ExampleFollowing is the code −const arr = [    {name: "Jack", age: "14"},    {name: "bob", age: ... Read More

JavaScript - Convert an array to key value pair

AmitDiwan
Updated on 30-Sep-2020 13:21:57

6K+ Views

Suppose we have an array like this −const arr = [ {"name": "Rahul", "score": 89},    {"name": "Vivek", "score": 88},    {"name": "Rakesh", "score": 75},    {"name": "Sourav", "score": 82},    {"name": "Gautam", "score": 91},    {"name": "Sunil", "score": 79}, ];We are required to write a JavaScript function that takes in one such array and constructs an object where name value is the key and the score value is their value.Use the Array.prototype.reduce() method to construct an object from the array.ExampleFollowing is the code −const arr = [    {"name": "Rahul", "score": 89},    {"name": "Vivek", "score": 88},    {"name": ... Read More

JavaScript - How to pick random elements from an array?

AmitDiwan
Updated on 30-Sep-2020 13:20:15

2K+ Views

Suppose, we have an array of literals that contains no duplicate elements like this −const arr = [2, 5, 4, 45, 32, 46, 78, 87, 98, 56, 23, 12];We are required to write a JavaScript function that takes in an array of unique literals and a number n.The function should return an array of n elements all chosen randomly from the input array and no element should appear more than once in the output array.ExampleFollowing is the code −const arr = [2, 5, 4, 45, 32, 46, 78, 87, 98, 56, 23, 12]; const chooseRandom = (arr, num = 1) ... Read More

Changing color randomly in JavaScript

AmitDiwan
Updated on 30-Sep-2020 13:19:12

391 Views

We are required to write a JavaScript function, let’s say randomColor that returns a randomly generated hex color every time it is called.ExampleFollowing is the code −const randomColor = () => {    let color = '#';    for (let i = 0; i < 6; i++){       const random = Math.random();       const bit = (random * 16) | 0;       color += (bit).toString(16);    };    return color; }; console.log(randomColor()); console.log(randomColor()); console.log(randomColor()); console.log(randomColor()); console.log(randomColor()); console.log(randomColor()); console.log(randomColor());OutputThis will produce the following output in console −#762b46 #cfa0bf #a20ee1 #c2f7e0 #5d5822 #380f30 #805408

Returning the highest number from object properties value – JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:37:02

739 Views

Suppose, we have an object that contains rating of a property over some criteria like this −const rating = {    "overall": 92,    "atmosphere": 93,    "cleanliness": 94,    "facilities": 89,    "staff": 94,    "security": 92,    "location": 88,    "valueForMoney": 92 }We are required to write a JavaScript function that takes in one such object and returns the key value pair that has the highest value.For example, for this very object, the output should be −const output = {    "staff": 94 };ExampleFollowing is the code −const rating = {    "overall": 92,    "atmosphere": 93,   ... Read More

Find indexes of multiple minimum value in an array in JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:35:48

600 Views

Suppose we have an array of numbers like this −const arr = [1, 2, 3, 4, 1, 7, 8, 9, 1];Suppose we want to find the index of the smallest element in the array i.e. 1 above.For this, we can simply use −const min = Math.min.apply(Math, arr); const ind = arr.indexOf(min);The above code will successfully set ind to 0, which indeed is correct.But what we want to achieve is that if there are more than one minimum elements in the array, like in the above array (three 1s), then we should return an array containing all the indices of minimum ... Read More

Writing a For Loop to Evaluate a Factorial - JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:34:47

1K+ Views

We are required to write a simple JavaScript function that takes in a Number, say n and computes its factorial using a for loop and returns the factorial.For example −factorial(5) = 120, factorial(6) = 720Maintain a count and a result variable, keep multiplying the count into result, simultaneously decreasing the count by 1, until it reaches 1And then finally we return the result.ExampleFollowing is the code −const num = 14; const factorial = num => {    let res = 1;    for(let i = num; i > 1; i--){       res *= i;    };    return res; }; console.log(factorial(num));OutputThis will produce the following output in console −87178291200

Normalize numbers in an object - JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:33:49

606 Views

Suppose, we have an object with strings mapped to numbers like this −const obj = {    num1: 45,    num2: 78,    num3: 234,    num4: 3,    num5: 79,    num6: 23 };We are required to write a JavaScript function that takes in one such object as the first argument and an array of strictly two numbers as the second argument.The second argument basically represents a range −[a, b] (b >= a)Our job is to normalize the object values according to the range.Therefore, the largest value of the object must become b and the smallest must become a. ... Read More

Implementing the Array.prototype.lastIndexOf() function in JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:32:11

281 Views

The lastIndexOf() function in JS returns the index of the very last occurrence of the element, passed into it as an argument, in the array, if it exists. If it does not exist the function returns -1.For example −[3, 5, 3, 6, 6, 7, 4, 3, 2, 1].lastIndexOf(3) would return 7.We are required to write a JavaScript function that have the same utility as the existing lastIndexOf() function.And then we have to override the default lastIndexOf() function with the function we just created. We will simply iterate from the back until we find the element and return its index.If we ... Read More

Sum similar numeric values within array of objects - JavaScript

AmitDiwan
Updated on 18-Sep-2020 13:31:07

892 Views

Suppose, we have an array of objects like this −const arr = [    {"firstName":"John", "value": 89},    {"firstName":"Peter", "value": 151},    {"firstName":"Anna", "value": 200},    {"firstName":"Peter", "value": 22},    {"firstName":"Anna", "value": 60} ];We are required to write a JavaScript function that takes in one such array and combines the value property of all those objects that have similar value for the firstName property.Therefore, for the above array, the output should look like −const output = [    {"firstName":"John", "value": 89},    {"firstName":"Peter", "value": 173},    {"firstName":"Anna", "value": 260} ];For each object, we will recursively find their similar objects(Similar objects ... Read More

Advertisements