Javascript Articles

Page 354 of 534

How to iterate an array of objects and build a new one in JavaScript ?

AmitDiwan
AmitDiwan
Updated on 20-Nov-2020 156 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
AmitDiwan
Updated on 20-Nov-2020 781 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

Read More

Array filtering using first string letter in JavaScript

AmitDiwan
AmitDiwan
Updated on 20-Nov-2020 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

Algorithm to sum ranges that lie within another separate range in JavaScript

AmitDiwan
AmitDiwan
Updated on 20-Nov-2020 155 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

Wrap object properties of type string with arrays - JavaScript

AmitDiwan
AmitDiwan
Updated on 09-Nov-2020 579 Views

For this, use Object.keys() along with reduce(). To display the result, we will also use concat().ExampleFollowing is the code −var details = { name: ["John", "David"], age1: "21", age2: "23" },    output = Object       .keys(details)       .reduce((obj, tempKey) =>          (obj[tempKey] = [].concat(details[tempKey]), obj), {}) console.log(output)  To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo302.js.OutputThis will produce the following output on console −PS C:\Users\Amit\javascript-code> node demo302.js { name: [ 'John', 'David' ], age1: [ '21' ], age2: [ '23' ] }

Read More

How to merge specific elements inside an array together - JavaScript

AmitDiwan
AmitDiwan
Updated on 09-Nov-2020 157 Views

Let’s say the following is our array −var values = [7,5,3,8,9,'/',9,5,8,2,'/',3,4,8];To merge specific elements, use map along with split().ExampleFollowing is the code −var values = [7,5,3,8,9,'/',9,5,8,2,'/',3,4,8]; var afterMerge = values.join('') .split(/(\d+)/). filter(Boolean). map(v => isNaN(v) ? v : +v); console.log(afterMerge);To run the above program, you need to use the following command −node fileName.js. Here, my file name is demo301.js.OutputThis will produce the following output on console −PS C:\Users\Amit\javascript-code> node demo301.js [ 75389, '/', 9582, '/', 348 ]

Read More

Get the correct century from 2-digit year date value - JavaScript?

AmitDiwan
AmitDiwan
Updated on 09-Nov-2020 368 Views

For this, you can use ternary operator based on some condition.ExampleFollowing is the code −const yearRangeValue = 18; const getCorrectCentury = dateValues => {    var [date, month, year] = dateValues.split("-");    var originalYear = +year > yearRangeValue ? "20" + year : "18" + year;    return new Date(date + "-" + month + "-" + originalYear).toLocaleDateString('en-GB') }; console.log(getCorrectCentury('10-JAN-19'));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo300.js.OutputThis will produce the following output on console −PS C:\Users\Amit\javascript-code> node demo300.js 1/10/2019

Read More

What is the &ldquo;get&rdquo; keyword before a function in a class - JavaScript?

AmitDiwan
AmitDiwan
Updated on 09-Nov-2020 496 Views

The get keyword can be used as a getter function like C#, Java and other technologies.We set a function with get like the following in a class −class Employee {    constructor(name) {    this.name = name;    }    get fullName() {       return this.name;    } }ExampleFollowing is the code displaying an example of get −class Employee {    constructor(name) {       this.name = name;    }    get fullName() {       return this.name;    } } var employeeObject = new Employee("David Miller"); console.log(employeeObject.fullName);To run the above program, you need to use ...

Read More

Replace multiple instances of text surrounded by specific characters in JavaScript?

AmitDiwan
AmitDiwan
Updated on 09-Nov-2020 349 Views

Let’s say the following is our string. Some text is surrounded by special character hash(#) −var values = "My Name is #yourName# and I got #marks# in JavaScript subject";We need to replace the special character with valid values. For this, use replace() along with shift().ExampleFollowing is the code −var values = "My Name is #yourName# and I got #marks# in JavaScript subject"; const originalValue = ["David Miller", 97]; var result = values.replace(/#([^#]+)#/g, _ => originalValue.shift()); console.log(result);To run the above program, you need to use the following command −node fileName.js. Here, my file name is demo298.js.OutputThis will produce the following output ...

Read More

What&#039;s the most efficient way to turn all the keys of an object to lower case - JavaScript?

AmitDiwan
AmitDiwan
Updated on 09-Nov-2020 224 Views

Let’s say the following is our object −var details = {    "STUDENTNAME": "John",    "STUDENTAGE": 21,    "STUDENTCOUNTRYNAME": "US" }As you can see above, the keys are in capital case. We need to turn all these keys to lower case. Use toLowerCase() for this.ExampleFollowing is the code −var details = {    "STUDENTNAME": "John",    "STUDENTAGE": 21,    "STUDENTCOUNTRYNAME": "US" } var tempKey, allKeysOfDetails = Object.keys(details); var numberOfKey = allKeysOfDetails.length; var allKeysToLowerCase = {} while (numberOfKey--) {    tempKey = allKeysOfDetails[numberOfKey];    allKeysToLowerCase[tempKey.toLowerCase()] = details[tempKey]; } console.log(allKeysToLowerCase);To run the above program, you need to use the following command −node ...

Read More
Showing 3531–3540 of 5,338 articles
« Prev 1 352 353 354 355 356 534 Next »
Advertisements