Javascript Articles

Page 366 of 534

Remove extra spaces in string JavaScript?

AmitDiwan
AmitDiwan
Updated on 07-Sep-2020 814 Views

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 More

How to modify key values in an object with JavaScript and remove the underscore?

AmitDiwan
AmitDiwan
Updated on 07-Sep-2020 602 Views

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 More

Remove '0','undefined' and empty values from an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 07-Sep-2020 297 Views

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 More

Display all the numbers from a range of start and end value in JavaScript?

AmitDiwan
AmitDiwan
Updated on 07-Sep-2020 923 Views

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 More

How could I write a for loop that adds up all the numbers in the array to a variable in JavaScript?

AmitDiwan
AmitDiwan
Updated on 07-Sep-2020 978 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

Any way to solve this without concatenating these two arrays to get objects with higher value?

AmitDiwan
AmitDiwan
Updated on 07-Sep-2020 75 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 a specific property in JavaScript?

AmitDiwan
AmitDiwan
Updated on 07-Sep-2020 598 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

JavaScript creating an array from JSON data?

AmitDiwan
AmitDiwan
Updated on 07-Sep-2020 969 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']

Read More

Accessing variables in a constructor function using a prototype method with JavaScript?

AmitDiwan
AmitDiwan
Updated on 07-Sep-2020 973 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 Miller

Read More

How to remove li elements on button click in JavaScript?

AmitDiwan
AmitDiwan
Updated on 07-Sep-2020 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

Read More
Showing 3651–3660 of 5,338 articles
« Prev 1 364 365 366 367 368 534 Next »
Advertisements