HarmonyOS 鸿蒙Next API11,如何更改数组中某个字段的全部值

发布于 1周前 作者 phonegap100 来自 鸿蒙OS

HarmonyOS 鸿蒙Next API11,如何更改数组中某个字段的全部值

我最开始没想定义class来做假数据,我想用Record来直接造假数据,但是好像是方式不对,没造出来~

然后就是更改数组中某个字段的值,原API9中是正常执行的,但是API11中原来的方法就会报错,大家有空帮我看看应该怎么修改~感谢

class testModel {
  time: number
  value: number

  constructor(time: number, value: number) {
    this.time = time
    this.value = value
  }
}

const testData: testModel[] = [
  new testModel(1010, 1),
  new testModel(1012, 5),
  new testModel(1013, 9),
  new testModel(1014, 6),
  new testModel(1015, 2)
]

@Entry
@Component
struct CanvasIndex {
  @State LineArray: testModel[] = testData
  @State yScale: number = 1

  aboutToAppear(): void {
    let maxValue = Math.max(...this.LineArray.map(item => item.value));
    let minValue = Math.min(...this.LineArray.map(item => item.value));

    this.yScale = 300 / (maxValue - minValue) //y轴值换算的比例

    //按照比例,更新数据中的value值
    this.LineArray = this.LineArray.map(item => ({ ...item, value: (item.value - minValue) * this.yScale })); 
  }

  build() {
    Column() {
      ForEach(this.LineArray, (item: testModel) => {

        Text(item.value.toString())

      }, (item: testModel) => JSON.stringify(item))
    }
  }
}

更多关于HarmonyOS 鸿蒙Next API11,如何更改数组中某个字段的全部值的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html

5 回复
class testModel {
  time: number
  value: number

  constructor(time: number, value: number) {
    this.time = time
    this.value = value
  }
}

const testData: testModel[] = [
  new testModel(1010, 1),
  new testModel(1012, 5),
  new testModel(1013, 9),
  new testModel(1014, 6),
  new testModel(1015, 2)
]

@Entry
@Component
struct CanvasIndex {
  @State LineArray: testModel[] = testData
  @State yScale: number = 1

  aboutToAppear(): void {
    let maxValue = Math.max(...this.LineArray.map(item => item.value));
    let minValue = Math.min(...this.LineArray.map(item => item.value));

    this.yScale = 300 / (maxValue - minValue) //y轴值换算的比例

    //按照比例,更新数据中的value值
    // this.LineArray = this.LineArray.map(item => ({ ...item, value: (item.value - minValue) * this.yScale }));
    // /*方案一:生成新的testModel*/
    // this.LineArray = this.LineArray.map(item => {
    //   const newItem = new testModel(item.time, (item.value - minValue) * this.yScale);
    //   return newItem;
    // });
    /*方案二:直接修改原对象*/
    this.LineArray.forEach((item, index) => {
      this.LineArray[index].value = (item.value - minValue) * this.yScale;
    });
  }

  build() {
    Column() {
      ForEach(this.LineArray, (item: testModel) => {

        Text(item.value.toString())

      }, (item: testModel) => JSON.stringify(item))
    }
  }
}

更多关于HarmonyOS 鸿蒙Next API11,如何更改数组中某个字段的全部值的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


是不是直接修改原对象更高效一些,对于代码的执行速度更友好一些~,

姓名: 张三
职位: 软件工程师
技能: Python, Java, C++

在HarmonyOS鸿蒙Next API11中,如果你需要更改数组中某个字段的全部值,可以使用map方法。map方法会遍历数组中的每个元素,并返回一个新数组,新数组中的每个元素都是原数组中对应元素经过处理后的结果。

假设你有一个对象数组,每个对象都有一个字段fieldName,你想将fieldName的值全部更改为newValue,可以按照以下方式操作:

const array = [
  { fieldName: 'value1', otherField: 'otherValue1' },
  { fieldName: 'value2', otherField: 'otherValue2' },
  { fieldName: 'value3', otherField: 'otherValue3' }
];

const newArray = array.map(item => ({
  ...item,
  fieldName: 'newValue'
}));

在这个例子中,newArray将会是一个新的数组,其中每个对象的fieldName字段都被更改为newValue,而其他字段保持不变。

如果你需要直接修改原数组而不是创建一个新数组,可以使用forEach方法:

array.forEach(item => {
  item.fieldName = 'newValue';
});

这样,原数组中的每个对象的fieldName字段都会被直接更改为newValue

在HarmonyOS 4.0(API 11)中,更改数组中某个字段的全部值可以通过map方法实现。假设你有一个对象数组,想要修改每个对象的某个字段值,可以使用以下代码:

let newArray = originalArray.map(item => {
  return { ...item, fieldName: newValue };
});

其中,originalArray是原始数组,fieldName是要修改的字段名,newValue是新值。map会返回一个新数组,原数组保持不变。

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!