javascript - 分隔数组内的元素

标签 javascript reactjs

我将 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"]
<小时/>

使用Array.prototype.join()

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/

相关文章:

javascript - 以 3 个子 div 为中心,宽度均为 100%?

javascript - 如何从经过 Firebase 身份验证的用户那里获取提供商访问 token ?

javascript - React-testing-library 渲染函数有什么问题?它对某些对象返回错误

javascript - AngularJS 表单未绑定(bind)到 $http 请求

javascript - 在 NodeJS 中将 MySQL 查询流式传输到 CSV 文件

reactjs - 如何将react-native-vector-icons与react-native-web一起使用?

javascript - this.props.dispatch().then 不是一个函数

reactjs - 在react中将数据传递给父组件

javascript - 将 Canvas 保存为 blob,然后将 blob 保存为文件

javascript - 如何让数组正确显示?