gpt4 book ai didi

javascript - 将 JSON 的 for 循环输出转换为字符串

转载 作者:行者123 更新时间:2023-12-01 02:58:39 24 4
gpt4 key购买 nike

如何通过 Javascript 中的 for 循环组合输出数据并将其设为字符串。

这是我的片段。

for (let index = 0; index < routes.length; index++) {
let element = routes[index];
meSpeak.speak(element.narrative, speakConfig);
}

结果

word1
word2
word3
word4

我只想将其作为一个字符串。像这样

word1 word2 word3 word4

最佳答案

只需使用join()

const routes = [{'narrative':'word 1'},{'narrative':'word2'},
{'narrative':'word 3'},{'narrative':'word 4'}];
let out = []

routes.map((el)=>(out.push(el.narrative)));
res = out.join(' ');

参见Demo

关于javascript - 将 JSON 的 for 循环输出转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46578508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com