我将 React 属性作为数组,我想用 -
分割这些元素。我尝试使用 .split('-') 但没有成功。
<Application
property={['name', 'lastName']}
/>
因此,名字和姓氏应该是: 姓名 - 姓氏
内部应用程序:
return (
<div key={`propName-${value}`}>
<span>{value}</span>
{' '} // the space is for: Name (space) LastName (space)
</div>
)
最佳答案
更新:
由于我们需要['name', '-', 'lastName']
,我们可以简单地使用 Array.prototype.splice() 添加它
The
splice()
method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
const data = ['name', 'lastName'];
data.splice(1, 0, '-')
console.log(data); // ["name", "-", "lastName"]
<小时/>
The
join()
method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string.
<Application
property={['name', 'lastName'].join('-')}
/>
关于javascript - 分隔数组内的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61078618/