Remove Characters from a String Contained in Another String with JavaScript

AmitDiwan
Updated on 07-Sep-2020 08:30:03

364 Views

Let’s say the following are our two strings −var originalName = "JOHNDOE"; var removalName = "JOHN"We need to remove the second string from first. For this, use replace() along with reduce().Exampleconst removeCharactersFromAString= (removalName,originalName)=>removalName.split('').reduce((obj,v)=>obj.replace(v,''),originalName); var originalName = "JOHNDOE"; var removalName = "JOHN" console.log(removeCharactersFromAString(removalName,originalName));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo93.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo93.js DOE

Un-Nesting Array of Objects in JavaScript

AmitDiwan
Updated on 07-Sep-2020 08:28:52

292 Views

To un-nest array of objects, use the concept of map(). Let’s say the following are our array of objects −const studentDetails = [    {       "studentId": 101,       "studentName": "John",       "subjectDetails": {          "subjectName": "JavaScript"       }    },    {       "studentId": 102,       "studentName": "David",       "subjectDetails": {          "subjectName": "MongoDB"       }    } ];We need to un-nest the subjectName and display the result. Following is the code −Exampleconst studentDetails = [   ... Read More

Multiple Variables Holding the Highest Value in JavaScript

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

444 Views

To fetch the highest value, use the Math.max() from JavaScript. Since, we want the one with maximum value, use the Object.values.Exampleconst studentMarksDetails= {    marks1:78,    marks2:69,    marks3:79,    marks4:74 } const maximumMarks = Math.max(...Object.values(studentMarksDetails)); console.log("The highest marks="+maximumMarks); for (const key of Object.keys(studentMarksDetails)){    if (studentMarksDetails[key] == maximumMarks) {       console.log(`The Object='${key}'`);       break;    } }To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo91.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo91.js The highest marks=79 The Object='marks3'Read More

Remove Extra Spaces in String using JavaScript

AmitDiwan
Updated on 07-Sep-2020 08:23:52

768 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

Modify Key Values in an Object with JavaScript and Remove Underscore

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

572 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' }

Remove 0, Undefined and Empty Values from an Array in JavaScript

AmitDiwan
Updated on 07-Sep-2020 08:21:15

241 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 Numbers from a Range in JavaScript

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

892 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, 19Read More

For Loop to Sum Numbers in an Array in JavaScript

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

945 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

54 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

Advertisements