gpt4 book ai didi

Javascript:删除浏览器控制台中输出数组的空白

转载 作者:行者123 更新时间:2023-12-03 01:45:57 24 4
gpt4 key购买 nike

是否有任何方法可以删除输出数组中不必要的空格。当我运行代码时,逗号和整数之间以及整数和括号之间都有空格。

var arr = [1,2,3,4,5];
console.log(arr);

输出

[ 1, 2, 3, 4, 5 ]

期望输出

[1,2,3,4,5]

最佳答案

console.logconsole.log是一个基本工具函数,具有非常明确的行为,您无法更改。

你唯一能做的就是使用技巧。 console.log字符串数组 的处理方式不同,因此您可以在显示数组之前对其进行转换。

此外,他们无法影响显示,因为它没有任何目的。您想在应用程序中使用 console.log 实现什么目的?

<小时/>

console.log 的创建是为了显示调试消息,而不是向普通用户显示数据。

例如在 node.js 中,为了在控制台中向用户显示数据,请使用:

process.stdout.write(your_string_in_your_own_format);
<小时/>

const arr = [1,2,3,4,5];

// Basic array
console.log(arr);

// Transform the arr into a string
console.log(JSON.stringify(arr));

关于Javascript:删除浏览器控制台中输出数组的空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50640052/

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