zishu's blog

zishu's blog

一个热爱生活的博主。https://zishu.me

Some easily overlooked points

  • 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]]
}
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.