Vue动态绑定Key值

HTML片段

<template>
    <div>
       vue动态添加对象Key值和value值
    </div>
</template>

script片段

<script>
    export default {
        data(){
            return{
              // 假数据  可以是接口返回的数据
                ImV:[
                    {Categorise:'假数据1',CategorID:'1',code:'100',Country:'大佬真棒'},
                    {Categorise:'假数据2',CategorID:'2',code:'200',Country:'大佬很棒'},
                    {Categorise:'假数据3',CategorID:'3',code:'300',Country:'大佬超棒'},
                    {Categorise:'假数据4',CategorID:'4',code:'400',Country:'大佬忒棒'},
                ],
            }
        },
        mounted(){
            this.MyRender()
        },
        methods:{
          // 只用到了里面其中两个数据
            MyRender(){
                let obj={}
                this.ImV.forEach((v,)=>{
                  obj[v.CategorID]=v.Country
                })
                console.log(obj) //打印结果{1: '大佬真棒', 2: '大佬很棒', 3: '大佬超棒', 4: '大佬忒棒'}
            }
        }
    }
</script>

 

posted @ 2022-02-19 17:47  热爱前端的5号机器  阅读(1565)  评论(0编辑  收藏  举报