AmitDiwan has Published 10744 Articles

Sorting an array object by property having falsy value - JavaScript

AmitDiwan

AmitDiwan

Updated on 30-Sep-2020 13:28:40

216 Views

Suppose, we have an array of objects like this −const array = [    {key: 'a', value: false},    {key: 'a', value: 100},    {key: 'a', value: null},    {key: 'a', value: 23} ];We are required to write a JavaScript function that takes in one such array and places all ... Read More

Counting occurrences of vowel, consonants - JavaScript

AmitDiwan

AmitDiwan

Updated on 30-Sep-2020 13:27:25

512 Views

We are required to write a JavaScript function that takes in a string which contains English alphabet, for example −const str = 'This is a sample string, will be used to collect some data';The function should return an object containing the count of vowels and consonants in the string i.e. ... Read More

Getting equal or greater than number from the list of numbers in JavaScript

AmitDiwan

AmitDiwan

Updated on 30-Sep-2020 13:25:50

785 Views

We are required to write a JavaScript function that takes in an array of numbers as the first argument and a single number as the second argument. The function should return an array of all the elements from the input array that are greater than or equal to the number ... Read More

Building a Map from 2 arrays of values and keys in JavaScript

AmitDiwan

AmitDiwan

Updated on 30-Sep-2020 13:24:32

6K+ Views

Suppose, we have two arrays −const keys = [0, 4, 2, 3, 1]; const values = ["first", "second", "third", "fourth", "fifth"];We are required to write a JavaScript function that takes in the keys and the values array and maps the values to the corresponding keys. The output should be −const ... Read More

Remove array duplicates by property - JavaScript

AmitDiwan

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 ... Read More

JavaScript - Convert an array to key value pair

AmitDiwan

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 ... Read More

JavaScript - How to pick random elements from an array?

AmitDiwan

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 ... Read More

Changing color randomly in JavaScript

AmitDiwan

AmitDiwan

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

386 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++){       ... Read More

Returning the highest number from object properties value – JavaScript

AmitDiwan

AmitDiwan

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

737 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 ... Read More

Find indexes of multiple minimum value in an array in JavaScript

AmitDiwan

AmitDiwan

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

598 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 = ... Read More

Advertisements