[英]suggestion for first and last name initials React hooks
I was wondering if you can help me solve this bug.我想知道你是否可以帮助我解决这个错误。 What I am looking for: if the organization name is one word then I want the first and second indices(ie Game = GA).
我在找什么:如果组织名称是一个单词,那么我想要第一个和第二个索引(即 Game = GA)。 And if the organization name is two or three names I want to return First and Second word initials (ie First Response == FR, or First Response Academy == FR. (The initials go inside of the Picture )
如果组织名称是两个或三个名称,我想返回第一个和第二个单词的缩写(即 First Response == FR,或 First Response Academy == FR。(图片中的缩写 go)
Here are some pictures of my the issue这是我的问题的一些图片
export const getInitials = (firstName, lastName) => {
return firstName.charAt(0).toUpperCase() + lastName.charAt(0).toUpperCase();
};
const renderProfileData = () => {
let firstName, lastName;
if (!organization) {
return <p>Loading...</p>;
} else {
const needHelp = Object.values(needs).some((val) => val === true);
const offerHelp = Object.values(objectives).some((val) => val === true);
const { address } = location;
const nameArr = name.split(" ");
if (nameArr.length < 2) {
firstName = nameArr[0];
lastName = firstName.split("").pop();
} else {
firstName = nameArr[0];
lastName = nameArr[1];
}
return (
<>
<UserInfoContainer>
{isOwner && <EditIcon src={edit} onClick={() => setDrawer(true)} />}
<ProfilePic
noPic={true}
initials={getInitials(firstName, lastName)}
/>
<UserInfoDesktop>
This example expects the string to be normalized with a capital first letter for each word (First Response Academy):此示例期望字符串使用每个单词的首字母大写进行规范化(First Response Academy):
// const name = ["First", "Last"].join(""); (if the name comes in an array) const fullName = "First Response Academy"; const name = "First Response"; const firstName = "First"; const denormalized = "fIrStNaMe LastName"; const toInitials = str => str // strip off capital letters (example: "First Last" ==> "FL").replace(/[^AZ]/g, "") // append the second character of the first word to end of this new string (example: "FL" ==> "FLI").concat(str.charAt(1).toUpperCase()) // limit this new string to 2 characters (example: "FLI" ==> "FL").substring(0, 2); [fullName, name, firstName, denormalized].forEach(w => { console.log(`${w}: ${toInitials(w)}`) });
The above example is brittle in that it expects the string to be normalized.上面的例子很脆弱,因为它期望字符串被规范化。 If it's not, it'll fail, so you may want to use this example instead:
如果不是,它将失败,因此您可能希望使用此示例:
const fullName = "fiRsT reSPoNsE AcAdEmY"; const name = "firsT responsE"; const firstName = "FirsT"; const normalized = "Firstname Lastname"; const toInitials = str => str // split string into array of strings (example: "fiRsT reSPoNsE AcAdEmY" ==> ["fiRsT", "reSPoNsE", "AcAdEmY"]).split(" ") // map over words and return a capitalized first letter of each word (example: ["fiRsT", "reSPoNsE", "AcAdEmY"] ==> ["F", "R", "A"]).map(c => c.charAt(0).toUpperCase()) // join letters to single string (example: ["F", "R", "A"] ==> "FRA").join("") // append second letter of first word to this new string (example: "FRA" ==> "FRAI").concat(str.charAt(1).toUpperCase()) // limit this new string to 2 characters (example: "FRAI" ==> "FR").substring(0, 2); [fullName, name, firstName, normalized].forEach(w => { console.log(`${w}: ${toInitials(w)}`) });
The javascript pop()
method removes and returns the last element in the array, since you want the second one, you can simply access it through its index which is 1 : javascript
pop()
方法删除并返回数组中的最后一个元素,因为你想要第二个,你可以简单地通过它的索引访问它1 :
lastName = firstName.split("")[1];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.