Javascript 中的关联数组中的值不可接受
Unacceptable value in associative array in Javascript
我在 JavaScript 的二维数组中设置关联数组的值。我使用了以下源代码:
var properties = {
"isBold": false,
"isItalic": false,
"isUnderline": false
};
// Creation of 2D array, 6x6
var listOfProperties = new Array(6);
for (var i = 0; i < listOfProperties.length; i++) {
listOfProperties[i] = new Array(6);
}
// Population of listOfProperties with properties
for (var row = 0; row < listOfProperties.length; row++) {
for (var col = 0; col < listOfProperties.length; col++) {
listOfProperties[row][col] = properties;
}
}
var property = listOfProperties[2][2];
property.isBold = true; // I would like to populate property isBold
console.log("property > " + property.isBold);
var property = listOfProperties[0][0];
console.log("property > " + property.isBold);
我创建了二维数组,并填充了 properties(关联数组,所有值均为假)。然后我为 *listOfProperties[2][2]**.
设置 true for isBold
但是为什么 isBold 在 listOfProperties[0][0] 中包含 true 而不是 假?
您需要填充对象副本的二维数组,而不是引用:
var properties = {
"isBold": false,
"isItalic": false,
"isUnderline": false
};
// Creation of 2D array, 6x6
var listOfProperties = new Array(6);
for (var i = 0; i < listOfProperties.length; i++) {
listOfProperties[i] = new Array(6);
}
// Population of listOfProperties with properties
for (var row = 0; row < listOfProperties.length; row++) {
for (var col = 0; col < listOfProperties.length; col++) {
listOfProperties[row][col] = Object.assign({}, properties);
}
}
var property = listOfProperties[1][1];
property.isBold = true; // I would like to populate property isBold
console.log("property > " + property.isBold);
var property = listOfProperties[0][0];
console.log("property > " + property.isBold);
有关详细信息,请查看 this。
我在 JavaScript 的二维数组中设置关联数组的值。我使用了以下源代码:
var properties = {
"isBold": false,
"isItalic": false,
"isUnderline": false
};
// Creation of 2D array, 6x6
var listOfProperties = new Array(6);
for (var i = 0; i < listOfProperties.length; i++) {
listOfProperties[i] = new Array(6);
}
// Population of listOfProperties with properties
for (var row = 0; row < listOfProperties.length; row++) {
for (var col = 0; col < listOfProperties.length; col++) {
listOfProperties[row][col] = properties;
}
}
var property = listOfProperties[2][2];
property.isBold = true; // I would like to populate property isBold
console.log("property > " + property.isBold);
var property = listOfProperties[0][0];
console.log("property > " + property.isBold);
我创建了二维数组,并填充了 properties(关联数组,所有值均为假)。然后我为 *listOfProperties[2][2]**.
设置 true for isBold但是为什么 isBold 在 listOfProperties[0][0] 中包含 true 而不是 假?
您需要填充对象副本的二维数组,而不是引用:
var properties = {
"isBold": false,
"isItalic": false,
"isUnderline": false
};
// Creation of 2D array, 6x6
var listOfProperties = new Array(6);
for (var i = 0; i < listOfProperties.length; i++) {
listOfProperties[i] = new Array(6);
}
// Population of listOfProperties with properties
for (var row = 0; row < listOfProperties.length; row++) {
for (var col = 0; col < listOfProperties.length; col++) {
listOfProperties[row][col] = Object.assign({}, properties);
}
}
var property = listOfProperties[1][1];
property.isBold = true; // I would like to populate property isBold
console.log("property > " + property.isBold);
var property = listOfProperties[0][0];
console.log("property > " + property.isBold);
有关详细信息,请查看 this。