gpt4 book ai didi

javascript - 如何在Javascript中打印console.log中的字符串等对象?

转载 作者:行者123 更新时间:2023-12-03 06:12:43 25 4
gpt4 key购买 nike

我想在控制台中打印函数 printNewObject() 的结果。如何在 Javascript 中使用 console.log 打印对象的结果?在输出中,我得到结果 Set {"1", "3", "4"},但我希望看起来像一个字符串,例如 134

代码如下:

window.onload = function(){

inputBox = document.getElementById("myText");
btn = document.getElementById('sub');

inputBox2 = document.getElementById("myText2");
btn2 = document.getElementById('sub2');

btn.addEventListener("click",function(event){
event.preventDefault();

toObject(inputBox.value);
});

btn2.addEventListener("click",function(event){
event.preventDefault();
printNewObject(inputBox.value, inputBox2.value);
});

function toObject(arr) {

var rv = {};

for (var i = 0; i < arr.length; ++i)
rv[i] = arr[i];

return rv;
}

function printNewObject(rv, number) {

var mySet = new Set(rv);

for (var i = 0; i <= rv.length; i++) {

if (rv[i] == number) {
mySet.delete(rv[i]);
}
}
console.log(mySet);
}
}

最佳答案

function printNewObject(rv, number) {

var mySet = new Set(rv);
var st = "";
for (var i = 0; i <= rv.length; i++) {

if (rv[i] == number) {
mySet.delete(rv[i]);
}
}
var array = Array.from(mySet);
for (var i = 0; i < array.length; i++){
st += array[i];
}
console.log(st);
}

关于javascript - 如何在Javascript中打印console.log中的字符串等对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39252284/

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