Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript-如何将for循环的结果存储到变量中?_Javascript_Loops_Variables - Fatal编程技术网

Javascript-如何将for循环的结果存储到变量中?

Javascript-如何将for循环的结果存储到变量中?,javascript,loops,variables,Javascript,Loops,Variables,这是我试图解析的数据,特别是索引中的“文本”值: (8) [a, a, a, a, a, a, a, a] 0:a {id: 2, articleId: "1156688772", type: 2, html: "<div id="DIV_1" style="box-sizing:border-box;color…ionUI.enableUserInput() }</script></div></div>", text: "Fyll ut felt

这是我试图解析的数据,特别是索引中的“文本”值:

(8) [a, a, a, a, a, a, a, a]
0:a {id: 2, articleId: "1156688772", type: 2, html: "<div id="DIV_1" style="box-sizing:border-box;color…ionUI.enableUserInput()    }</script></div></div>", text: "Fyll ut feltene under for å starte en chat: Alle f…tionSession.conversationUI.enableUserInput()    }"}
1:a {id: 3, type: 5, date: 1529671597923, html: "", text: ""}
2:a {id: 4, type: 1, html: "hvordan får jeg tak i en sånn bombrikke", text: "hvordan får jeg tak i en sånn bombrikke"}
3:a {id: 5, articleId: "1140973712", type: 2, html: "Gratulerer med ny bil! Velg den avtalen som passer for deg:", persistentOptions: Array(2), …}
4:a {id: 6, type: 1, html: "er det gratis med elbil", text: "er det gratis med elbil"}
5:a {id: 7, articleId: "1158440612", type: 2, html: "<p>El-biler passerer forel&oslash;pig gratis i&nbs…g i Norge,&nbsp;hvis man har AUTOPASS-avtale.</p>", text: "El-biler passerer foreløpig gratis i de fleste bomanlegg i Norge, hvis man har AUTOPASS-avtale."}
6:a {id: 8, type: 1, html: "a med dere", text: "a med dere"}
7:a {id: 9, articleId: "1158681852", type: 2, html: "<p>Jeg beklager, jeg forst&aring;r ikke sp&oslash;…. Kan du si det p&aring; en annen m&aring;te?</p>", text: "Jeg beklager, jeg forstår ikke spørsmålet ditt. Kan du si det på en annen måte?"}
length:8
__proto__
:Array(0)
(8)[a,a,a,a,a,a,a]

0:a{id:2,articleId:“1156688772”,type:2,html:“您可以映射索引2及以后的值并连接每个文本。或者在不连接的情况下获取数组供以后处理

var chat = nanorep.floatingWidget.$refs.core.conversationSession.entries,
    chatHistory = function() {
        return chat.slice(2).map(({ text }) => text).join(' ');
    };

console.log(chatHistory());

由于这会在一个循环中生成多个日志,您希望它返回到什么程度?可能是:作为一个值数组?您可以将其存储在数组或对象中,但这取决于您的需要,我们需要更多info@deceze,Darmian Peralta我必须尝试两种方法,但作为对象是首选的。因为我必须将其传递给HTML元素。然后,而不是
console.log(chatHistory());
我想尝试类似的方法:
chatHistory2=chatHistory();chatHistory3=chatHistory.replace(/\./g,“\n”)
你可以做你需要的事情并处理结果。它真的对你有用吗?它完美地给出了结果,但我没有将结果传递给console.log,而是必须找到一种方法将代码的结果收集到一个新变量。这是因为我只能选择将信息作为变量名传递。例如:
customField:chatHistory
对此有何想法?请在您的帖子中提供示例数据。@NinaScholz这也变得越来越类似于我之前的问题,但这不是我的意图,我正试图做一些不同的事情,但无论如何,您再次提出了解决方案,因此再次感谢您,也感谢所有提出想法的人。我同意今天我有了新东西,却没有打算这么做:)
var chat = nanorep.floatingWidget.$refs.core.conversationSession.entries,
    chatHistory = function() {
        return chat.slice(2).map(({ text }) => text).join(' ');
    };

console.log(chatHistory());