How to push value into an array

I have a blank array. i want to push value into blank array from another array.I can not do.

when i push the value into the array.there showing an error.

Below my code:

for(let i=0;i<this.selectcity.length;i++){
        
        this.test.push(this.selectcity[i].available_at);
      	
      }

Error:
abc
please help me anyone

let array1 = [0, 1, 2];
let array2 = [3, 4, 5];
let bothTogether = [...array1, array2];
console.log(bothTogether); // [0, 1, 2, 3, 4, 5];

let otherWay = array1.concat(array2);
console.log(otherWay); // [0, 1, 2, 3, 4, 5];

Your “test” array variable is either undefined or it is not present in the scope where you are running the loop.
Delare it globally or inside required function using test = [];.

Ensure that this.selectcity is not undefined.

You could’ve also neglected to initialize the array:

selectcity = [];
1 Like

this.selectcity already define. is not undefined.below the class am define the selectcity array
like that:
public selectcity:any;

Try using:

public selectcity: any = [];

The error message says “test” is undefined. This means test variable is not defined. Use test = [] before using it.
This is not related with selectcity.

Apologies. I seem to have overlooked that part. :slight_smile:

the this.test undefined problem is gone but when i print the array of the value in html.there showing blank value
here my html code:

<ion-select multiple="false" cancelText="Cancel" okText="Okay!">
      <ion-option value="selectcity" selected="true">Select City</ion-option>
       <ion-option *ngFor="let name of test">{{name.available_at}}</ion-option>
    </ion-select>
1 Like

JavaScript is a terrible language. Angular exacerbates some of that. You need to follow coding rules that protect you from bugs like this.

Any time you have an object property, initialize it, even if it is just {}.
Any time you have an array property, initialize it, even if it is just [].
I have yet to see a situation where any is necessary in app code, so don’t ever use it.

Options are things that the user might select. Does this make sense in that context?

The way you are accessing it name.available_at tells that name is an object and test is an array of objects.

Try inspecting your code in the loop you have mentioned. Using breakpoints you can identify if test array contains correct data (array of objects).

Make sure the key name available_at is correct.

1 Like

Great point, and looking at the OP it obviously isn’t, because the code that attempts to initialize test is already dereferencing avaialable_at.

:smiley: I complitly missed that.
I could have mentioned just to use this.test.push(this.selectcity[i]);.

map is much more readable anyway, IMHO.

Thank you sir .its working fine.

The entire point of marking something as the solution is that somebody with a similar problem knows exactly where to look. Do you honestly think that the post you marked as the “solution” qualifies?

  <ion-card-content>

    <div class="dFlexAlignItemCenter">

        <ion-icon name="add-circle-outline" class="iconSize fontSize"></ion-icon>

     

      <ion-item lines="none">

        <ion-label class="fontSize ion-padding-start" position="floating">Address</ion-label>

        <ion-input [(ngModel)]="address1"></ion-input>

      </ion-item>

    </div>

  </ion-card-content>

my ts file:

ngOnInit(){

this.address1= 

  {name:'', flat:'', locality:'', nickName:'' }



  this.address.push(this.address1);

  console.log(this.address);

}

when i enter the address it has to be store in the array (address)
but iam not getting the value , PLease suggest me any idea how to achiev this