编程语言
首页 > 编程语言> > JavaScript:把数组变成对象,添加动态key

JavaScript:把数组变成对象,添加动态key

作者:互联网

<template>
  <div>
   把数组变成对象,添加动态key
  </div>

</template>

<script>
  export default {
    name: "",
   props: {
   	dataSource: {}
   }, 
    data() {
      return {
		  data1:[11,22,33,44,55,66,77,88],
		  data2:{}
	  }
    },
    created() {},
    mounted() {
		this.data2 = this.createKeys(this.data1);
		console.log(this.data2);
	},
    methods: {
		createKeys(arr){
			const obj = {};
			arr.forEach((value,index)=>{
				obj['key' + index] = value;
			})
			return obj;
		}
	}
  }
</script>

<style scoped>
  
</style>

效果:

 

标签:index,arr,obj,key,JavaScript,数组,data1,data2
来源: https://blog.csdn.net/lilycheng1986/article/details/121565115