0

push and splice is not working here. this is my model. i need this model to build my tables. splice remove everything and push doesn't do anything.

export class Parameter {

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

}

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

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

this is my function

  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}});
  }

1 Answers1

0

array.push return A Number which representing the new length of the array.

array.splice return A new Array, containing the removed items (if any).

So, the problem here is that you overwrite your array with the values ​​returned by these two methods.

The solution is that you don't have to assign them to your table, use directly push and splice, because push and splice already mutates the original array :

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

And not forget to initialize your table this._parameters[pIndex].wx (check if is already initialized or not)

Community
  • 1
  • 1
aira
  • 81
  • 4