[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>前言
在vue中,遍历数组和对象的方式略有不同,不能完全以数组或对象的遍历方式给对方使用并获取数据。为了记录以及以后方便查看,现在对其进行整理。
<>数组遍历
以数组
array = [1,2,3,4,5]
为例
数组的遍历有以下几种
* 获取数组的长度进行遍历 for (let i = 0; i<array.length;i++) { Console.log("遍历后的数据",array
[i]); }
* 使用foreach遍历 array.foreach((index) => { Console.log("遍历后的数组:",array[index]);
Console.log("下标:",index); })
* Object.key()遍历 Object.key(array).foreach((index) => { Console.log("遍历后数组:"
,array[index]); Console.log("下标:",index); })
* map遍历 array.map((index) => { Console.log("遍历后数组:",array[index]); Console.log
("下标:",index); })
* for… in遍历 for (let inedx in array){ Console.log("遍历后数组:",array[index]);
Console.log("下标:",index); }
* for…of遍历 for (let index of array) { Console.log("遍历后数组:",array[index]);
Console.log("下标:",index); }
<>对象遍历
以对象
let obj = [ { name: "张三", age: "15", }, { name: "李四", age: "26", } ]
为例
* for … in … 遍历 for (let key in obj){ Console.log("下标:",key); Console.log(
"遍历后每条数据:",obj[key]); Console.loh("遍历后每天数据姓名属性值:",obj[key].name); }
* Object.key()遍历 Object.keys(obj).forEach((key) => { Console.log("下标:",key);
Console.log("遍历后每条数据:",obj[key]); Console.loh("遍历后每天数据姓名属性值:",obj[key].name); })
<>结语
以上为vue遍历数组和对象的方式。