Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当原始字符串是姓氏,firstname时,如何在文本字段中显示firstname?React/Spfx_Javascript_Reactjs_Spfx - Fatal编程技术网

Javascript 当原始字符串是姓氏,firstname时,如何在文本字段中显示firstname?React/Spfx

Javascript 当原始字符串是姓氏,firstname时,如何在文本字段中显示firstname?React/Spfx,javascript,reactjs,spfx,Javascript,Reactjs,Spfx,我想简单地从字符串中提取firstname。Firstname和lastname用逗号分隔 我知道这将涉及一个内置函数来查找逗号,然后提取逗号后的所有字符串 我希望它显示在文本字段中 public _getUser() { //getUser sets the state for multiple properties of the user. sp.web.currentUser.get().then((user) => { this.setState({

我想简单地从字符串中提取firstname。Firstname和lastname用逗号分隔

我知道这将涉及一个内置函数来查找逗号,然后提取逗号后的所有字符串

我希望它显示在文本字段中

 public _getUser() { //getUser sets the state for multiple properties of the user. 

    sp.web.currentUser.get().then((user) => {

      this.setState({
        CurrentUserTitle: user.Title,
        ExtractedFirstName: 


上面的代码是我用来获取登录用户的代码。我想必须创建一个函数,但我会在函数中放入什么,然后设置为状态?

如果名字和姓氏的形状如下:
firstName,lastName
,您只需在逗号上拆分字符串并提取结果数组的第一部分:

this.setState({
  // ...
  ExtractedFirstName: firstNameAndLastNameCombined.split(',')[0]
如果名字和姓氏的组合有一些空白,还可以修剪结果:

this.setState({
  // ...
  ExtractedFirstName: firstNameAndLastNameCombined.split(',')[0].trim()
以下是两个方面的示例:

const firstNameAndLastNameCombined='Cool,Example';
console.log(
firstNameAndLastNameCombined.split(',')[0]
)//酷
const firstname和lastname与空格组合='Cool,Example';
console.log(
firstname和lastname与space.split(',')[0].trim()组合
)//酷