Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Javascript Articles
Page 366 of 534
Remove extra spaces in string JavaScript?
To remove extra spaces, you need to use trim() along with regular expressions. Following is our string with spaces before applying trim() −var sentence="My name is John Smith ";Now, we will remove the extra spaces as in the below code −Examplevar sentence="My name is John Smith "; console.log("The actual value="); console.log(sentence); var originalSentence = sentence.replace(/\s+/g, '').trim(); var afterRemovingTheSpace=originalSentence.trim(); console.log("After removing the spaces="); console.log(afterRemovingTheSpace);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo90.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo90.js The actual value= My name is John Smith After removing ...
Read MoreHow to modify key values in an object with JavaScript and remove the underscore?
In order to modify the key values, you need to use regular expressions along with Object.fromEntries().Examplevar underscoreSpecifyFormat = str => str.replace(/(_)(.)/g, (_, __, v) => v.toUpperCase()), JsonObject = { first_Name_Field: 'John', last_Name_Field: 'Smith'}, output = Object.fromEntries(Object.entries(JsonObject).map(([key, value]) => [underscoreSpecifyFormat(key), value])); console.log("The JSON Object="); console.log(output);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo89.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo89.js The JSON Object= { firstNameField: 'John', lastNameField: 'Smith' }
Read MoreRemove '0','undefined' and empty values from an array in JavaScript
To remove ‘0’. ‘undefined’ and empty values, you need to use the concept of splice(). Let’s say the following is our array −var allValues = [10, false, 100, 150 ,'', undefined, 450, null]Following is the complete code using for loop and splice() −Examplevar allValues = [10, false, 100, 150 ,'', undefined, 450, null] console.log("Actual Array="); console.log(allValues); for (var index = 0; index < allValues.length; index++) { if (!allValues[index]) { allValues.splice(index, 1); index--; } } console.log("After removing false, undefined, null or ''..etc="); console.log(allValues);To run the above program, you need to use the following ...
Read MoreDisplay all the numbers from a range of start and end value in JavaScript?
Let’s say the following is our start value −var startValue=10;Let’s say the following is our end value −var endValue=20;Use the for loop to fetch numbers between the start and end value −Examplevar startValue=10; var endValue=20; var total=''; function printAllValues(startValue, endValue){ for(var start=startValue;start < endValue ;start++){ total=total+start+", "; } } printAllValues(startValue, endValue) var allSequences = total.slice(0, -1); console.log(allSequences);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo87.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo87.js 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
Read MoreHow could I write a for loop that adds up all the numbers in the array to a variable in JavaScript?
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 MoreAny way to solve this without concatenating these two arrays to get objects with higher value?
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 MoreFilter array of objects by a specific property in JavaScript?
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 MoreJavaScript creating an array from JSON data?
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']
Read MoreAccessing variables in a constructor function using a prototype method with JavaScript?
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 Miller
Read MoreHow to remove li elements on button click in JavaScript?
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
Read More