将需要缓存的字符串数组转换为对象数组,然后通过维护对象数组来保存缓存值。以下是代码示例:
在组件中定义对象数组:
cachedValues = [{id: 'array1', values: []}, {id: 'array2', values: []}, {id: 'array3', values: []}];
在需要缓存的地方,从字符串数组转换为对象数组:
const newArray1 = arr1.map((val) => {return {value: val};});
const newArray2 = arr2.map((val) => {return {value: val};});
const newArray3 = arr3.map((val) => {return {value: val};});
this.cachedValues[0].values = newArray1;
this.cachedValues[1].values = newArray2;
this.cachedValues[2].values = newArray3;
在需要使用缓存值的地方,从对象数组转换为字符串数组:
const cachedArr1 = this.cachedValues[0].values.map((obj) => obj.value);
const cachedArr2 = this.cachedValues[1].values.map((obj) => obj.value);
const cachedArr3 = this.cachedValues[2].values.map((obj) => obj.value);