我想使数组中的每一项都显示在不同的行中,但我的“\n”不起作用。我把它放错地方了吗?我试过把它放在其他地方,但似乎没有什么不同。我的函数目标是遍历数组。
var employees = [];
employees[0] = "Zak";
employees[1] = "Jessica";
employees[2] = "Mark";
employees[3] = "Fred";
employees[4] = "Sally";
var showEmployee = function (names) {
'use strict';
showEmployee = employees;
return names;
};
showEmployee(employees);
window.console.log(showEmployee + "\n");
最佳答案
你在数组的末尾添加了一个换行符,你需要在数组中的每个元素之间添加它(使用连接)
var employees = [];
employees[0] = "Zak";
employees[1] = "Jessica";
employees[2] = "Mark";
employees[3] = "Fred";
employees[4] = "Sally";
var showEmployee = function (names) {
'use strict';
showEmployee = employees;
return names;
};
showEmployee(employees);
window.console.log(showEmployee.join("\n"));
关于javascript - 为什么我的数组中的项目不显示在单独的行中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54991207/