【问题标题】:splice or push problem with array of object with angular具有角度的对象数组的拼接或推送问题
【发布时间】:2020-05-11 08:34:24
【问题描述】:

推送和拼接在这里不起作用。这是我的模型。我需要这个模型来构建我的表格。 splice 删除所有内容,而 push 不执行任何操作。

export class Parameter {

  constructor(
    public asset: string,
    public wx: IWx[]
  ) {
  }

}

export interface IWx {
  [key: string]: IWxValue;
}

export interface IWxValue {
  yellowValue: any;
  redValue: any;
}

这是我的功能

  ajouteWx(pIndex: number, wxIndex: number) {
    console.log(pIndex);
    console.log(wxIndex);
    this._parameters[pIndex].wx = this._parameters[pIndex].wx.push({hello: {yellowValue: 5, redValue: 2}});
    this._parameters[pIndex].wx = this._parameters[pIndex].wx.splice(wxIndex, 0, {hello: {yellowValue: 5, redValue: 2}});
  }

【问题讨论】:

  • 我忘了说我想添加一个 wx 数组

标签: angular object weather splice weatherdata


【解决方案1】:

array.push 返回一个表示数组新长度的数字。

array.splice 返回一个新数组,其中包含已删除的项目(如果有)。

所以,这里的问题是你用这两种方法返回的值覆盖了你的数组。

解决方案是你不必将它们分配给你的表,直接使用push和splice,因为push和splice已经改变了原始数组:

this._parameters[pIndex].wx.push({hello: {yellowValue: 5, redValue: 2}});
this._parameters[pIndex].wx.splice(wxIndex, 0, {hello: {yellowValue: 5, redValue: 2}});

别忘了初始化你的表 this._parameters[pIndex].wx (检查是否已经初始化)

【讨论】:

    猜你喜欢
    • 2020-10-15
    • 1970-01-01
    • 2021-12-07
    • 2019-04-17
    • 1970-01-01
    • 2020-10-28
    • 2023-02-05
    • 1970-01-01
    • 2017-12-04
    相关资源
    最近更新 更多