js对对象操作

遍历对象

 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,
  },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值