gpt4 book ai didi

javascript - 为什么我的数组中的项目不显示在单独的行中?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:40:09 25 4
gpt4 key购买 nike

我想使数组中的每一项都显示在不同的行中,但我的“\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/

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