<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>
效果: