【问题标题】:How can I display these values with spaces to separate the words?如何使用空格显示这些值以分隔单词?
【发布时间】:2021-12-28 08:11:28
【问题描述】:

如果我要console.log(JSON.stringify(selected["1"]?.others)),这就是它显示的内容:

["Cars","Books","Necklaces"]

但是,在屏幕上显示这个,这将显示所有数据,但是,每个单词之间没有空格。显示是这样的

汽车书籍项链

selected["1"]?.others 中的others 是一个数组。

这就是它在 Firestore 中的样子:

【问题讨论】:

    标签: javascript reactjs firebase google-cloud-firestore


    【解决方案1】:

    由于这是一个字符串值数组,您可以像这样使用join 方法:

    selected["1"]?.others.join(' ')
    

    【讨论】:

    • Masksimovic 我尝试在join的参数中输入<br/>,它只会显示[object]
    • @RavenousRed 假设你正在使用 React,如果你想在元素之间用 <br /> 创建一个新行,那么只需像这样遍历数组:{selected['1']?.others.map((item, index) => <div key={index}>{item} <br /></div>)} 但我建议不要使用 @ 987654328@,最好只为others 数组中的每个项目渲染ulli
    猜你喜欢
    • 1970-01-01
    • 2010-12-01
    • 1970-01-01
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    相关资源
    最近更新 更多