Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/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 在循环内字符串的开头连接_Javascript - Fatal编程技术网

Javascript 在循环内字符串的开头连接

Javascript 在循环内字符串的开头连接,javascript,Javascript,我有以下连接到字符串“errors”结尾的代码: this.errors+=`[${new Date().toLocaleString()}]`; this.errors+=response.data.message; this.errors+=“\n---------\n\n”; 这将生成以下字符串: [30/12/2020 14:48:00] Error 1 ----------------- [30/12/2020 14:49:10] Error 2 -----------------

我有以下连接到字符串“errors”结尾的代码:

this.errors+=`[${new Date().toLocaleString()}]`;
this.errors+=response.data.message;
this.errors+=“\n---------\n\n”;
这将生成以下字符串:

[30/12/2020 14:48:00] Error 1
-----------------

[30/12/2020 14:49:10] Error 2
-----------------
但是,我需要在字符串的开头连接,以便最近的错误始终位于顶部:

[30/12/2020 14:49:10] Error 2
-----------------

[30/12/2020 14:48:00] Error 1
-----------------
我怎样才能做到这一点呢?

试试这个:

const message = `[${new Date().toLocaleString()}] ` 
  + response.data.message
  + "\n-----------------\n\n";
this.errors = message + this.errors;

可以利用数组来存储错误

const errors=[];
函数加法器(消息){
错误.unshift(`${new Date().toLocaleString()}${message}`);
}
函数displayErrors(){
console.log(errors.join(“\n--------------\n\n”);
}
加法器(“错误1”);
加法器(“错误2”);
加法器(“错误3”);
显示错误()
this.errors = "\n-----------------\n\n" + this.errors;
this.errors = response.data.message + this.errors;
this.errors = `[${new Date().toLocaleString()}] ` + this.errors;