将接收的字符串转为键值对对象

103 阅读1分钟
接收一组数组字符串,每个字符串内以空格分开前后内容,需要将字符串以空格为中心分割成键值对形式的对象

22.png 转化后 11.png

import {  ref, watchEffect } from "vue";
const props = defineProps({
  subScores: {
    type: Array, 
    default: [],
  },
});

let description = ref({});
//页面初次加载就监听
watchEffect(() => {
  for (const item of props.subScores) {
    item.split(" ").forEach((item, index, array) => {
      if (index % 2 === 0) {
        // 如果是偶数索引,则当前项为键
        description.value[item] = array[index + 1]; // 将下一项作为值添加到对象中
      }
    });
  }
});