Front End Technology Articles - Page 319 of 745

Function to check two strings and return common words in JavaScript

AmitDiwan
Updated on 20-Nov-2020 13:17:17

868 Views

We are required to write a JavaScript function that takes in two strings as arguments. The function should then check the two strings for common characters and prepare a new string of those characters.Lastly, the function should return that string.The code for this will be −Exampleconst str1 = "IloveLinux"; const str2 = "weloveNodejs"; const findCommon = (str1 = '', str2 = '') => {    const common = Object.create(null);    let i, j, part;    for (i = 0; i < str1.length - 1; i++) {       for (j = i + 1; j

Filter an array containing objects based on another array containing objects in JavaScript

AmitDiwan
Updated on 20-Nov-2020 10:36:43

13K+ Views

Suppose we have two arrays of objects like these −const arr1 = [{id:'1', name:'A'}, {id:'2', name:'B'}, {id:'3', name:'C'}, {id:'4', name:'D'}]; const arr2 = [{id:'1', name:'A', state:'healthy'}, {id:'3', name:'C', state:'healthy'}];We are required to write a JavaScript function that takes in two such arrays. Our function should return a new filtered version of the first array (arr1 in this case) that contains only those objects with a name property that are not contained in the second array (arr2 in this case) with the same name property.Therefore, the output, in this case, should look like −const output = [{id:'2', name:'B'}, {id:'4', name:'D'}];ExampleThe code ... Read More

How to sort an object in ascending order by the value of a key in JavaScript?

Disha Verma
Updated on 11-Mar-2025 16:34:54

889 Views

Sorting an object in ascending order by the value of a key is a common task in JavaScript. Objects are data structures, which are a collection of key-value pairs. Since objects in JavaScript are unordered collections, sorting them directly is not possible. This article will guide you on how to sort an object in ascending order by the value of a key in JavaScript. Understanding the Concept Suppose we have the following object: const obj = { "sub1": 56, "sub2": 67, "sub3": 98, "sub4": 54, ... Read More

Extract arrays separately from array of Objects in JavaScript

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 : 0 }];We are required to write a JavaScript function that takes in one such array and extracts a separate array for each object property.Therefore, one array for the name property of each object, one for total and one for value. If there existed more properties, we would have separated more ... Read More

Checking if a key exists in a JavaScript object

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

493 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 of JavaScript, we might want to check for the existence of key in an object like this −const obj = { name: 'Rahul' };if(!obj['fName']){}orif(obj['fName'] === undefined){}These both are incorrect ways. Why?Because in this case there happens to be no 'fName' key, but suppose there existed a 'fName' which was deliberately ... Read More

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

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

171 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
Updated on 20-Nov-2020 10:26:40

185 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 basis of the first array so that the content property of objects are matched with the strings of the first array.Therefore, for the above arrays the output should look like −const output = [    {"id":3, "content":"this"},    {"id":1, "content":"is"},    {"id":2, "content":"my"},    {"id":4, "content":"custom"},    {"id":5, "content":"order"} ];ExampleThe ... Read More

Return indexes of greatest values in an array in JavaScript

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

423 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 code for this will be −const arr = [10, 5, 4, 10, 5, 10, 6]; const findGreatestIndices = arr => {    const val = Math.max(...arr);    const greatest = arr.reduce((indexes, element, index) => {       if(element === val){          return indexes.concat([index]);       } else {          return indexes;       };    }, []);    return greatest; } console.log(findGreatestIndices(arr));OutputAnd the output in the console will be −[ 0, 3, 5 ]

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

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

346 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 generate.Then at last our function should return the greatest all random numbers generated.ExampleThe code for this will be −const range = [15, 26]; const count = 10; const randomBetweenRange = ([min, max]) => {    const random = Math.random() * (max - min) + min;    return random; }; const ... Read More

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

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

314 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, 1+2+3+4+5 = 15We will use the below formula to solve this problem −Sum of all natural number upto n =((n*(n+1))/2)ExampleThe code for this will be −const num = 5; const sumUpto = num => {    const res = (num * (num + 1)) / 2;    return res; }; ... Read More

Advertisements