how to combine first name and last name in javascript?
You should use this
keyword which is a reference to the current object.
function Person(){
}
Person.prototype.fname=function(){ return "abc"}
Person.prototype.lname=function(){ return "lop"}
Person.prototype.fullname=function(){ return this.fname() + this.lname()}
var p = new Person();
console.log(p.fullname())
Split First name and Last name using JavaScript
You should use the String.prototype.split()
method:
'Paul Steve Panakkal'.split(' '); // returns ["Paul", "Steve", "Panakkal"]
You can use it this way:
'Paul Steve Panakkal'.split(' ').slice(0, -1).join(' '); // returns "Paul Steve"
'Paul Steve Panakkal'.split(' ').slice(-1).join(' '); // returns "Panakkal"
So in common:
var firstName = fullName.split(' ').slice(0, -1).join(' ');
var lastName = fullName.split(' ').slice(-1).join(' ');
Flip first name and last name to last name first name
You can use split()
to separate the lines and then use a loop.
var splitName = document.getElementById("splitName");
splitName.onclick = function() {
document.getElementById("result").innerHTML = '';
const value = document.getElementById("fullName").value;
value.split('\n').forEach(fullname => {
var spaceIndex = fullname.indexOf(" ");
var firstname;
var lastname;
if (spaceIndex == -1) {
lastname = fullname;
lastname = "";
} else {
firstname = fullname.substring(0, spaceIndex);
lastname = fullname.substr(spaceIndex + 1);
}
document.getElementById("result").innerHTML += lastname + ", " + firstname + "<br>";
});
};
<div>
<textarea cols="20" rows="5" id="fullName">Jane Doe
Joe Doe
Joan Doe
Jenny van Doe</textarea>
</div>
<div id="splitName" class="hwbutton">Reverse</div>
<div id="result"></div>
Javascript Concatenate FirstName, LastName and Code
There is no definition for 'str' used within your alert line..
alert("Your login Code for the store is: " + firstname[0] + lastname[0] + (generateCode()));
The above should work. It also looks like you have a stray } after the if block. You should probably also be doing more error checking for the user input, if the user doesn't enter a value at the prompt for example, you will get undefinedundefined#### where #### is the generated code.
Combine Fullname in ES6
[firstName, middleName, lastName].filter(Boolean).join(" ")
should work just fine.
Since the empty string is falsy and Boolean(x)
will compute the truthiness of x
, .filter(Boolean)
will drop it.
How to get first and last name when someone enters full name in Javascript?
It's just not possible without additional information. How could you ever know if someone has 2 first names or 2 last names without manually reading each value to make a decision on where the split is. The only way to solve this is to have 2 separate input fields
First Name and Last Name Combine search in mongoDB
You can use $regexMatch
if you are using latest mongodb version 4.2
db.collection.find({
"$expr": {
"$regexMatch": {
"input": { "$concat": ["$first", " ", "$last"] },
"regex": "a", //Your text search here
"options": "i"
}
}
})
MongoPlayground
Related Topics
For Loop With Two Array in JavaScript
Increase Counter Value Upon Button Click
Javascript Regex: Replace Hyphen Between Characters Only (Not Spaces)
Uncheck a Checkbox and Another Checkbox Will Untick
Keep Order of Objects Inside a Json String After They Are Parsed
Vue Router This.$Router.Push Not Working on Methods
Charts.Js Graph Not Scaling to Canvas Size
Open a New Tab on Button Click in Angularjs
Push Objects into an Array in Reactjs
How to Iterate Using Ngfor Loop Map Containing Key as String and Values as Map Iteration
Why Flatlist Is Not Updating Dynamically in React Native
Delete Span Tag Along With Text in HTML Permanently
How to Format Numbers in Vuejs
Write a Auto-Fill and Submit Web Form Program
How to Reload Datatable by Ajax (Jquery)