In JavaScript I have an array. I’m using push() to add array elements, but when I manipulate said elements, each array element is changed, when only one should be. My code is:
// Arrays to hold the elements that have been created (added to the document).
create.element_array = []
create.element_array["name"] = []
create.element_array["name"]["properties"] = []
var element_properties =
{
// Default all to 0.
borderTopStyle: 0, borderRightStyle: 0, borderBottomStyle: 0, borderLeftStyle: 0,
borderTopWidth: 0, borderRightWidth: 0, borderBottomWidth: 0, borderLeftWidth: 0,
borderTopColor: 0, borderRightColor: 0, borderBottomColor: 0, borderLeftColor: 0,
}
// Testing purposes. These will be added dynamically in production version.
create.element_array["name"].push("default_header");
create.element_array["name"]["properties"].push(element_properties);
create.element_array["name"].push("default_body");
create.element_array["name"]["properties"].push(element_properties);
create.element_array["name"].push("default_footer");
create.element_array["name"]["properties"].push(element_properties);
// Evidence that each array element is changed with index manipulation.
create.element_array["name"]["properties"][0].borderTopStyle = "dashed";
console.log(create.element_array["name"]["properties"][0]);
console.log(create.element_array["name"]["properties"][1]);
console.log(create.element_array["name"]["properties"][2]);
The result I expect from this is that ONLY index 0 should be changed, but this is not the case. The above console.log lines will output the following:
Object
borderBottomColor: 0
borderBottomStyle: 0
borderBottomWidth: 0
borderLeftColor: 0
borderLeftStyle: 0
borderLeftWidth: 0
borderRightColor: 0
borderRightStyle: 0
borderRightWidth: 0
borderTopColor: 0
borderTopStyle: "dashed"
borderTopWidth: 0
__proto__: Object
library.js:8
Object
borderBottomColor: 0
borderBottomStyle: 0
borderBottomWidth: 0
borderLeftColor: 0
borderLeftStyle: 0
borderLeftWidth: 0
borderRightColor: 0
borderRightStyle: 0
borderRightWidth: 0
borderTopColor: 0
borderTopStyle: "dashed"
borderTopWidth: 0
__proto__: Object
library.js:8
Object
borderBottomColor: 0
borderBottomStyle: 0
borderBottomWidth: 0
borderLeftColor: 0
borderLeftStyle: 0
borderLeftWidth: 0
borderRightColor: 0
borderRightStyle: 0
borderRightWidth: 0
borderTopColor: 0
borderTopStyle: "dashed"
borderTopWidth: 0
__proto__: Object
library.js:8
Why is borderTopStyle being changed for every element, when I explicitly define index 0 to be the only index that gets changed?
You are adding the same reference to the
element_propertiesobject to each spot in the array. This means that when you try to change it, you’re changing the value of the object pointed-to by that reference.You might want to instead use a constructor the properties object:
Then, something like this: