AmitDiwan has Published 10744 Articles

Extract arrays separately from array of Objects in JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Nov-2020 10:33:19

1K+ Views

Suppose, we have an array of objects like this −const arr = [{    name : 'Client 1',    total: 900,    value: 12000 }, {    name : 'Client 2',    total: 10,    value: 800 }, {    name : 'Client 3',    total: 5,    value : ... Read More

Checking if a key exists in a JavaScript object

AmitDiwan

AmitDiwan

Updated on 20-Nov-2020 10:30:11

461 Views

We are required to illustrate the correct way to check whether a particular key exists in an object or not. Before moving on to the correct way let's first examine an incorrect way and see how actually its incorrect.Way 1: Checking for undefined value (incorrect way)Due to the volatile nature ... Read More

Check if an array is growing by the same margin in JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Nov-2020 10:28:03

139 Views

We are required to write a JavaScript function that takes in an array of numbers. Our function should return true if the difference between all adjacent elements is the same positive number, false otherwise.ExampleThe code for this will be −const arr = [4, 7, 10, 13, 16, 19, 22]; const growingMarginally = arr => {    if(arr.length

Use array as sort order in JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Nov-2020 10:26:40

153 Views

const sort = ["this", "is", "my", "custom", "order"]; const myObjects = [    {"id":1, "content":"is"},    {"id":2, "content":"my"},    {"id":3, "content":"this"},    {"id":4, "content":"custom"},    {"id":5, "content":"order"} ];We are required to write a JavaScript function that takes in two such arrays and sorts the second array of objects on the ... Read More

Return indexes of greatest values in an array in JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Nov-2020 10:24:27

392 Views

We are required to write a JavaScript function that takes in an array of Numbers. The array may contain more than one greatest element (i.e., repeating greatest element).We are required to write a JavaScript function that takes in one such array and returns all the indices of the greatest element.ExampleThe ... Read More

Generate n random numbers between a range and pick the greatest in JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Nov-2020 10:22:46

317 Views

We are required to write a JavaScript function that takes in an array of two numbers as the first argument, this array specifies a number range within which we can generate random numbers.The second argument will be a single number that specifies the count of random numbers we have to ... Read More

Function to add up all the natural numbers from 1 to num in JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Nov-2020 10:21:45

270 Views

We are required to write a JavaScript function that takes in a number, say num.Then our function should return the sum of all the natural numbers between 1 and num, including 1 and num.For example, if num is −const num = 5;Then the output should be −const output = 15;because, ... Read More

Randomly shuffling an array of literals in JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Nov-2020 10:20:07

136 Views

We are required to write a JavaScript function that takes in an array of literals.Then the function should shuffle the order of elements in any random order inplace.ExampleThe code for this will be −const letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']; const unorderArray = arr => {   ... Read More

Search a complex object by id property in JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Nov-2020 10:19:03

416 Views

Suppose we have a complex JSON Object like this −const obj = {    "id": "0001",    "fieldName": "sample1",    "fieldValue" "0001",    "subList": [       {          "id": 1001,          "fieldName": "Sample Child 1",          "fieldValue": "1001",   ... Read More

Replace alphabets with nth forward alphabet in JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Nov-2020 10:15:49

448 Views

We are required to write a JavaScript function that takes in an alphabet string and a number, say n. We should then return a new string in which all the characters are replaced by respective alphabets at position n alphabets next to them.For example, if the string and the number ... Read More

Advertisements