- If the value is null when listening to an object, an error will be thrown. Set the default value to {}.
- Using "for in" can only iterate over objects. Writing "i in 4" is incorrect.
- Dynamically retrieve array elements for concatenation and passing as parameters.
var arr = [
"Unit1",
"Unit2",
"Unit3",
"Unit4",
"Unit5"
]
// 1. Array []
for (var i = 0; i < arr.length; i++) {
this.defaultData.hostUnitList.push({
[arr[i]]: this.defaultData[arr[i]]
});
}
// 2. Object {}
for (var i = 0; i < arr.length; i++) {
this.defaultData.hostUnitList[arr[i]] = this.defaultData[arr[i]]
}