For Loop to Sum Numbers in an Array in JavaScript

AmitDiwan
Updated on 07-Sep-2020 08:14:02

953 Views

At first, declare and initialize the total variable with 0 and then you need to iterate over all the array and extract all the values of array and add up with the updated total variable.Let’s say the following is our array with numbers −var listOfValues = [10, 3, 4, 90, 34, 56, 23, 100, 200];Examplevar listOfValues = [10, 3, 4, 90, 34, 56, 23, 100, 200]; var total = 0; for(let index = 0; index < listOfValues.length ; index++){    total=total+listOfValues[index]; } console.log("Total Values="+total);To run the above program, you need to use the following command −node fileName.js.Here, my file name ... Read More

Capitalize Only the First Letter After Colon with Regex and JavaScript

AmitDiwan
Updated on 07-Sep-2020 08:13:12

1K+ Views

To capitalize only the first letter, use the concept of regular expression along with toUpperCase(). Since the toUpperCase() capitalize the entire string, we need to use Regex to only capitalize the first letter after colon.Let’s say the following is our string −var objectValues='fullName: john Smith';Here’s the complete code to capitalize only the first letter after colon −Examplevar objectValues='fullName: john Smith'; function capitalizeFirstAfterTheColon(value){    return value.replace(/([:\?]\s+)(.)/g, function(data) {       return data.toUpperCase();    }); } console.log(capitalizeFirstAfterTheColon(objectValues));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo85.js.OutputThis will produce the following output −PS ... Read More

Get Objects with Higher Value from Two Arrays

AmitDiwan
Updated on 07-Sep-2020 08:12:08

58 Views

To get objects with specific property, use the concept of reduce() on both arrays individually. You don’t need to concatenate. Let’s say the following are our objects with student name and student marksvar sectionAStudentDetails = [    {studentName: 'John', studentMarks: 78},    {studentName: 'David', studentMarks: 65},    {studentName: 'Bob', studentMarks: 98} ]; let sectionBStudentDetails = [    {studentName: 'John', studentMarks: 67},    {studentName: 'David', studentMarks: 89},    {studentName: 'Bob', studentMarks: 97} ];Following is the code to implement reduce() on both and fetch the object with higher value (marks) −Examplevar sectionAStudentDetails = [    {studentName: 'John', studentMarks: 78},    {studentName: 'David', ... Read More

Filter Array of Objects by Specific Property in JavaScript

AmitDiwan
Updated on 07-Sep-2020 08:10:43

574 Views

Use the concept of map() along with ternary operator (?). Following are our array of objects −let firstCustomerDetails = [    {firstName: 'John', amount: 100},    {firstName: 'David', amount: 50},    {firstName: 'Bob', amount: 80} ];    let secondCustomerDetails = [    {firstName: 'John', amount: 400},    {firstName: 'David', amount: 70},    {firstName: 'Bob', amount: 40} ];Let’s say, we need to filter array of objects by amount property. The one with the greatest amount is considered.Examplelet firstCustomerDetails = [    {firstName: 'John', amount: 100},    {firstName: 'David', amount: 50},    {firstName: 'Bob', amount: 80} ]; let secondCustomerDetails = [   ... Read More

Create an Array from JSON Data in JavaScript

AmitDiwan
Updated on 07-Sep-2020 08:09:27

941 Views

To create an array from JSON data, use the concept of map() from JavaScript. Let’s say the following is our data −const studentDetails =[    {       name : "John"    },    {       name : "David"    },    {       name : "Bob"    } ];Following is the code to create an array from the above data −Exampleconst studentDetails =[    {       name : "John"    },    {       name : "David"    },    {       name : "Bob"    } ]; const ListOfStudentName = studentDetails.map(({name:actualValue})=>actualValue); console.log(ListOfStudentName);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo82.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo82.js [ 'John', 'David', 'Bob']

Null Coalescing Operator in JavaScript

AmitDiwan
Updated on 07-Sep-2020 08:08:05

206 Views

Yes, JavaScript now supports the “null coalescing” operator, but you can also use the concept of logical OR (||). The syntax is as follows −var anyVariableName=null; var anyVariableName=yourVariableName || yourActualValue;Examplevar fullName=null; console.log("The full name is="+fullName); var actualName=fullName || "David Miller"; console.log("The full name is="+actualName);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo81.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo81.js The full name is=null The full name is=David Miller

Access Variables in Constructor Function Using Prototype Method in JavaScript

AmitDiwan
Updated on 07-Sep-2020 08:05:35

933 Views

For this, use a “prototype”. JavaScript objects inherit properties and methods from a prototype. For accessing variables, we have also used the “this” in JavaScript.Examplefunction Customer(fullName){    this.fullName=fullName; } Customer.prototype.setFullName = function(newFullName){    this.fullName=newFullName; } var customer=new Customer("John Smith"); console.log("Using Simple Method = "+ customer.fullName); customer.setFullName("David Miller"); console.log("Using Prototype Method = "+customer.fullName);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo79.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo79.js Using Simple Method = John Smith Using Prototype Method = David MillerRead More

Remove LI Elements on Button Click in JavaScript

AmitDiwan
Updated on 07-Sep-2020 08:04:37

4K+ Views

Let’s say the following is our Unordered List (ul) −    JavaScript Remove        MySQL Remove        MongoDB Remove        Java Remove Above, you can see the “Remove” button with every li element. On clicking this button, you can remove any li element.Following is the code to remove li elements on button click;Example Live Demo Document Remove the subjects The Subjects are as follows: JavaScript Remove MySQL Remove MongoDB Remove Java Remove    var allSubjectName = document.querySelectorAll(".subjectName");    for (var index = 0; index

Join Map Values into a Single String with JavaScript

AmitDiwan
Updated on 07-Sep-2020 08:01:39

3K+ Views

You can use the concept of Map(). In JavaScript, map has the key value concept in which key must be unique.We also need to use the join() method to join Map values. Following is the code −Examplelet queryStringAppendWithURL = new Map(); queryStringAppendWithURL.set("firstParamter", ["name=John", "age=23", "countryName=US"]); queryStringAppendWithURL.set("secondParamter", ["subjectName=JavaScript", "Marks=91"]); let appendValue = Array.from(queryStringAppendWithURL.values()).map(value => value.join('?')).join('?'); console.log("The appended value is="+appendValue);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo78.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo78.js The appended value is=name=John?age=23?countryName=US?subjectName=JavaScript?Marks=91Read More

Conditionally Change Object Property with JavaScript

AmitDiwan
Updated on 07-Sep-2020 08:00:24

1K+ Views

To conditionally change object property, use the logical AND operator ( &&).If both the operands are non-zero, then the condition becomes true in JavaScript’s logical AND operator.Examplelet marksDetails = { Mark1: 33, Mark2: 89 }, isBacklog = false; console.log("Result when backlog is set to false==="); console.log({ ...marksDetails, ...isBacklog === true && { Mark1: 33 }}); isBacklog = true; console.log("Result when backlog is set to true==="); console.log({ ...marksDetails, ...isBacklog === true && { Mark1: 93 }});To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo77.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> ... Read More

Advertisements