我创建了一个对象,其中有2个数组。当我尝试在控制台中打印其元素时,它被打印了两次。
const err1 = {
"type": new Array(),
"desc": new Array()
}
err1.type.push("a","b");
err1.desc.push("ananas","banana");
var errD = new String();
var errT = new String();
err1.type.forEach(function(el, ind){
errT += `Type: ${el}`;
errD += `Description: ${err1.desc[ind]}`;
console.log(`ERROR: \n${errT}\n${errD}`);
});
安慰:
ERROR:
Type: a
Description: ananas
---------------------------- (console line)
ERROR:
Type: aType: b
Description: ananasDescription: banana
字符串是否已经在数组中还是我将其推入数组都没关系。我也尝试通过for循环来控制台它们-结果是相同的。
为什么会这样呢?
这是因为要添加,请尝试以下操作:
const err1 = {
"type": new Array(),
"desc": new Array()
}
err1.type.push("a","b");
err1.desc.push("ananas","banana");
var errD = new String();
var errT = new String();
err1.type.forEach(function(el, ind){
errT = `Type: ${el}`;
errD = `Description: ${err1.desc[ind]}`;
console.log(`ERROR: \n${errT}\n${errD}`);
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句