简体   繁体   English

在 ReactJS 中的数组 object 之后添加空格

[英]Add space after array object in ReactJS

I'm mapping an array in React where I'm outputting tags that are held in an array.我在 React 中映射一个数组,在其中输出保存在数组中的标签。 I've got commas after every object, but would like a space as well.我在每个 object 之后都有逗号,但也想要一个空格。

Here is the mapping code:这是映射代码:

{
    result &&
        result.map(project => {
            if (project.item) project = project.item;
            return (
                <ProjectStyling key={project.id}>
                    <h4>Project Tags:</h4>
                    <p>{(project ? " " : ", ") + project.keywords}</p>
                </ProjectStyling>
            );
        });
}

At the moment it looks like "Bob,Foo,Bar" where I want it to look like "Bob, Foo, Bar"目前它看起来像 "Bob,Foo,Bar" 我希望它看起来像 "Bob, Foo, Bar"

Note there is already a space after the comma in the jsx注意jsx中逗号后面已经有空格了

It's showing up without spaces because that's the default string representation of an array.它显示时没有空格,因为这是数组的默认字符串表示形式。 You can use the join function to join each element with whatever separator you want.您可以使用连接function 使用您想要的任何分隔符连接每个元素。

In your case, I think a comma followed by a space is what you want.就您而言,我认为逗号后跟空格是您想要的。

<h4>Project Tags:</h4>
<p>{project.keywords.join(', ')}</p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM