首页 > 编程知识 正文

vue遍历json数组,vue怎么遍历json数组

时间:2023-05-03 15:50:50 阅读:276827 作者:2741

如果是一个数组包含对象且每个都对象内部固定包含child子数组,命名规范的话,使用for in就行了,去判断每一集下面还有没有child: [ ],

但是现在碰到不规则的json对象就很棘手,参数完全不确定,只能使用递归了,先贴代码:

watch:{ colorData: { handler() { console.log("颜色变更========", this.componentList); function findColorText(obj) { for (var i in obj) { //判断是否为object 有就再从此层级开始循环 if (typeof (obj[i]) == "object") { findColorText(obj[i]); //递归遍历所有下层对象 } else { if(i === 'color'){ //找出参数名为color obj[i] = 'blue'; // 当前循环对象的参数为color 赋值为blue console.log(i,"找到所有名为color的参数---",obj[i]); } } } } findColorText(this.componentList) } }, deep:true},

watch中监听获取json对象,调用递归方法即可。

个人纪录,仅供参考!

快三三期必中口诀找出参数名为color obj[i] = 'blue'; // 当前循环对象的参数为color 赋值为blue console.log(i,"找到所有名为color的参数---",obj[i]); } } } } findColorText(this.componentList) } }, deep:true},

watch中监听获取json对象,调用递归方法即可。

个人纪录,仅供参考!

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。