Object Oriented Programming Articles

Page 384 of 588

How to convert an object into an array in JavaScript?

AmitDiwan
AmitDiwan
Updated on 01-Sep-2020 450 Views

To convert an object into an array, use push() in JavaScript. Following is the code −ExamplestudentDetails=   [       { studentName:"Chris",studentMarks:34},       { studentName:"David",studentMarks:89} ] var convertIntoArray = []; for (var i = 0; i < studentDetails.length; i++) {    convertIntoArray.push(studentDetails[i].studentName); } console.log(convertIntoArray);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo35.js.OutputThis will produce the following output.PS C:\Users\Amit\JavaScript-code> node demo35.js [ 'Chris', 'David' ]

Read More

How can I cut a string after X characters in JavaScript?

AmitDiwan
AmitDiwan
Updated on 01-Sep-2020 738 Views

To cut a string after X characters, use substr() function from JavaScript. Following is the JavaScript code wherein we are cutting the string after 9th character −Examplevar myName="JohnSmithMITUS"; console.log("The String="+myName) var afterXCharacter = myName.substr(0, 9) + "\u0026"; console.log("After cutting the characters the string="+afterXCharacter);Above, the unicode “\u0026” will replace all the characters with &(“\u0026”).To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo34.js.OutputThis will produce the following output.PS C:\Users\Amit\JavaScript-code> node demo34.js The String=JohnSmithMITUS After cutting the characters the string=JohnSmith&

Read More

Finding content of arrays on the basis of specific property in JavaScript?

AmitDiwan
AmitDiwan
Updated on 01-Sep-2020 167 Views

For this, you can use find() along with a map(). Let’s say we have student records with name, rollno and subject.Examplevar firstObject= [    { "FirstName": "David", "RollNo": "105", "Subject": "MongoDB" },    { "FirstName": "Mike", "RollNo": "110", "Subject": "JavaScript"} ]; var secondObject= [    { "FirstName": "Bob", "RollNo": "101", "Subject": "Java" },    { "FirstName": "John", "RollNo": "110", "Subject": "MySQL" } ]; var output = firstObject.map(first=>(secondObject.find(second=>second.RollNo==first.R ollNo) || first)); console.log(output);To run the above program, you need to use the following command −node fileName.jsHere, my file name is demo33.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo33.js [   ...

Read More

How can we separate the special characters in JavaScript?

AmitDiwan
AmitDiwan
Updated on 01-Sep-2020 355 Views

To separate the special character, use the concept of match() with Regular Expression. The syntax is as follows −yourStringName.flatMap(anyVariableName => yourVariableName.match(/\w+|\W+/g));Let’s say, the following is our array with special characters in between values −var allNames = ['John-Smith', 'David', 'Carol%Taylor'];Let’s now see how to separate text with special characters. Following is the code −Examplevar allNames = ['John-Smith', 'David', 'Carol%Taylor']; var output = allNames.flatMap(obj => obj.match(/\w+|\W+/g)); console.log(output);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo32.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo32.js [    'John', '-',    'Smith', 'David',   ...

Read More

Check if string begins with punctuation in JavaScript

AmitDiwan
AmitDiwan
Updated on 01-Sep-2020 757 Views

Let’s say we have the following strings. One of them is beginning with a question mark i.e. punctuation −var sentence1 = 'My Name is John Smith.' var sentence2 = '? My Name is John Smith.'We need to check whether any of the above two sentences begin with punctuation. To check if string begins with punctuation, the code is as follows −Examplevar punctuationDetailsRegularExpression=/^[., :!?]/ var sentence1 = 'My Name is John Smith.' var output1 = !!sentence1.match(punctuationDetailsRegularExpression) if(output1==true)    console.log("This ("+sentence1+") starts with a punctuation"); else    console.log("This ("+sentence1+") does not starts with a punctuation"); var sentence2 = '? My Name is ...

Read More

Remove the whitespaces from a string using replace() in JavaScript?

AmitDiwan
AmitDiwan
Updated on 01-Sep-2020 218 Views

Let’s say the following is our string with whitespace −var fullName=" John Smith ";Use replace() and set Regex in it to remove whitespaces.Examplefunction removeSpacesAtTheBeginningAndTheEnd(name) {    return name.toString().replace(/^\s+|\s+$/g,''); } var fullName=" John Smith "; var valueAfterRemovingSpace=removeSpacesAtTheBeginningAndTheEnd(fullName) console.log(valueAfterRemovingSpace);To run the above program, you need to use the following command −node fileName.js.Here my file name is demo208.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo208.js John Smith

Read More

Best way to find two numbers in an array whose sum is a specific number with JavaScript?

AmitDiwan
AmitDiwan
Updated on 01-Sep-2020 492 Views

Let’s say the following is our array −var numbers = [10, 3, 40, 50, 20, 30, 100]We need to search two numbers from the above array elements, whose sum is 80.For this, use simple for loop with if condition.Examplefunction specificPairsOfSumOfTwoNumbers(numbers, totalValue)    {       var storeTwoNumbersObject = {}       for(var currentNumber of numbers)       {          if(storeTwoNumbersObject[currentNumber])          {             return {                firstNumber: totalValue-currentNumber, secondNumber:currentNumber}             }         ...

Read More

JavaScript How to get all &#039;name&#039; values in a JSON array?

AmitDiwan
AmitDiwan
Updated on 01-Sep-2020 4K+ Views

Let’s say the following is our JSON array −var details = [    {       "customerDetails": [             {                "customerName": "John Smith",                "customerCountryName": "US"             }       ]    },    {       "customerDetails": [          {             "customerName": "David Miller",             "customerCountryName": "AUS"          }       ]    },    { ...

Read More

JavaScript Remove all &#039;+&#039; from array wherein every element is preceded by a + sign

AmitDiwan
AmitDiwan
Updated on 01-Sep-2020 195 Views

Let’s say the following is our array with elements preceded by a + sign −var studentNames = [    '+John Smith',    '+David Miller',    '+Carol Taylor',    '+John Doe',    '+Adam Smith' ];To remove the + sign, the code is as follows −ExamplestudentNames = [    '+John Smith',    '+David Miller',    '+Carol Taylor',    '+John Doe',    '+Adam Smith' ]; console.log("The actual array="); console.log(studentNames); studentNames = studentNames.map(function (value) {    return value.replace('+', ''); }); console.log("After removing the + symbol, The result is="); console.log(studentNames);To run the above program, you need to use the following command −node fileName.js.Here my file ...

Read More

Access previously iterated element within array.map in JavaScript?

AmitDiwan
AmitDiwan
Updated on 31-Aug-2020 360 Views

Let’s say the following is our array −var details = [    {subjectId:110, subjectName: 'Java' },    {subjectId:111, subjectName: 'Javascript' },    {subjectId:112, subjectName: 'MySQL' },    {subjectId:113, subjectName: 'MongoDB' } ];Now, use the concept of map(). The code is as follows −Examplevar details = [    {subjectId:110, subjectName: 'Java' },    {subjectId:111, subjectName: 'JavaScript' },    {subjectId:112, subjectName: 'MySQL' },    {subjectId:113, subjectName: 'MongoDB' } ]; var output = details.map((detailsObject, index) => {    var tempObject = {};    tempObject.subjectId= detailsObject.subjectId;    tempObject.subjectName = detailsObject.subjectName;    const getThePreviousObject = index != 0 ? details[index-1] : null;    tempObject.previousSubjectName = ...

Read More
Showing 3831–3840 of 5,877 articles
« Prev 1 382 383 384 385 386 588 Next »
Advertisements