How to push a value to array

This is silly question, but I am wandering.

I have blank array and I want to push a value into the array.

my code: cloud.ts

    this.connected_cloud = [
      {
        name:'connected_csp[0]',
        value:this.csp.connectedCspList[0]
      }
    ];
    this.connectable_cloud = [
      {
        name:'connected_csp[0]',
        value: this.csp.connectableCspList[0]
      },
      {
        name:'connected_csp[1]',
        value: this.csp.connectableCspList[1]
      },
      {
        name:'connected_csp[2]',
        value: this.csp.connectableCspList[2]
      },
      {
        name:'connected_csp[3]',
        value: this.csp.connectableCspList[3]
      }
    ];

  openCloud(i) {
    if (i == 0) {

    } else if (i == 1) {
      this.dropbox.login().then((success) => {
        this.navCtrl.setRoot(CloudPage);
        alert('dropbox connected');
        this.csp.pushConnectedCsp('Dropbox');
        this.csp.popConnectedCsp(i);
      }, (err) => {
        alert(err);
      });
    } else if (i == 2) {

    } else if (i == 3) {

    } else if (i == 4) {

    } else if (i == 5) {

    }
  }



csp.ts (provider)

  public connectableCspList:Array<string>=['Google Drive', 'Dropbox', 'pCloud', 'MediaFire'];
  public connectedCspList:Array<string>=[null];

  pushConnectedCsp(csp){
    this.connectedCspList.push(csp);
  }
  popConnectableCsp(i){
    this.connectableCsp.pop();       // I do not know this part also'
  }

cloud

My expectation: When I choose dropbox in a list, dropbox.login() method executed (<-this part has no problem) and then ‘Dropbox’ is added in the ‘Connected’ list and removed in the ‘Connectable’ list.

I searched some topics about push and pop method but I couldn’t apply it to my code.

I see this topic: [How to push value into an array] but I couldn’t also.

Considering I’m new at JavaScript and Ionic framework, please give me an advice. Thanks

Maybe this is a just an initialization problem?

Instead of

 public connectedCspList:Array<string>=[null];

have your try

// Create an empty array
public connectedCspList:Array<string> = new Array(); 

Then I think you do correctly

this.connectedCspList.push(csp);

To remove, instead of pop, I think you could maybe try splice

 let index: number = this.connectedCspList.indexOf('thestringyouaresearching');
 if (index > -1) {
      this.connectedCspList.splice(index, 1);
 }
1 Like

It’s working as I wished. Thank you so much.

1 Like