遍历对象
for(let [key,value] of Object.entries(fruits)){
console.log(key, value)
}
也可以 遍历li
<body>
<ul>
<li>阿萨</li>
<li>时代</li>
<li>地方</li>
</ul>
</body>
<script>
const lis = document.querySelectorAll('li');
for(let li of lis){
li.addEventListener('click', function() {
this.classList.toggle('completed')
})
}
const names = Array.from(lis, li => li.textContent)
//一下是分步方法
//将nodeList转换为li数组
// const lisArr = Array.from(lis);
// //将li数组的内容提取出来转换为数组
// const names = lisArr.map(li => li.textContent)
console.log(names)
</script>
给对象新增属性
formatDtails(details: AttendDetail[]){
let formatObj:any = {};
for(let i = 0; i<details.length; i++){
let currentDetail = details[i];
let date = currentDetail.date;
let status = this.getStatus(currentDetail);
formatObj[date] = status
}
return formatObj;
}
定义对象
let columns: { title: string, prop: string }[] = []
创建对象
let checkpoints = location.coords.map((coord: any) => {
let item: PatrolDroneStationPlanCheckpoint = <any>{
id: null,
stationId: null,
stationPlanId: null,
lat: coord[1],
lng: coord[0],
alt: 0,
orderNo: 0
};
return item;
});
获取的checkpoints
对象和json字符串互转
对象转json字符串
JSON.stringify(object)
json字符串转对象
JSON.parse(JsonString);
避免对象一开始是空报错的方法
<el-form-item label="地址:" prop="address">
<el-input type="text" v-model="editingAirShiftStation.location.address" autocomplete="off"></el-input>
</el-form-item>
location: {
address: <any>null,
districtCode: <any>null,
lat: <any>null,
lng: <any>null,
alt: <any>null,
},