当我想将数组转换为 JSON 时,下面的代码有问题。请在下面查看我的代码:
<html>
<head>
</head>
<body style="text-align:center;" id="body">
<p id="GFG_UP1" style="font-size: 16px;">
</p>
<p id="GFG_UP2" style="font-size: 16px;">
</p>
<button onclick="gfg_Run()">
Convert
</button>
<p id="GFG_DOWN1" style="color:red;
font-size: 20px; font-weight: bold;">
</p>
<p id="GFG_DOWN2" style="color:red;
font-size: 20px; font-weight: bold;">
</p>
<script>
var el_up1 = document.getElementById("GFG_UP1");
var el_up2 = document.getElementById("GFG_UP2");
var el_down1 = document.getElementById("GFG_DOWN1");
var el_down2 = document.getElementById("GFG_DOWN2");
var array1 = [34, 24, 31, 48];
var array2 = [23, 43, 65, 52];
var numberArray1 = [];
var numberArray2 = [];
for (i = 0; i < array1.length; i++) {
numberArray1[i] = "number" + i + ':' + array1[i];
}
el_up1.innerHTML = "Array = [" + array1 + "]";;
function gfg_Run() {
el_down2.innerHTML =
JSON.stringify(Object.assign(numberArray1));
}
for (i = 0; i < array2.length; i++) {
numberArray2[i] = "number" + i + ':' + array2[i];
}
el_up2.innerHTML = "Array = [" + array2 + "]";;
function gfg_Run() {
el_down2.innerHTML =
JSON.stringify(Object.assign(numberArray2));
}
</script>
</body>
</html>
正在给出输出:
["number0:23","number1:43","number2:65","number3:52"]
不是两个数组都被转换
就像它应该同时提供输出:
["number0:34","number1:24","number2:31","number3:48"]
["number0:23","number1:43","number2:65","number3:52"]