0

Whenever i create array at initialization it works fine. i can access it using Array[index]. but whenever i create an empty array array = [] , and then push array data using push , i cannot access it using Array[index].

testAdd: Address[]= [{add: "D/102", loc: 'thane'},{add: "Room No, 106", loc: 'Mumbai'}];
Add : Address[] = [];
this.Add.push({add:"102,ajaynagar", loc 'andheri'});
console.log(this.testAdd[0])  //results:-  {add: "D/102", loc: 'thane'}
console.log(this.Add[0])    // results:- undefined

i want to fill up the array using push command from the data coming from database. it fills up correctly but retrieval shows undefined

3
  • what you got in this.Add? Commented Jan 28, 2019 at 11:26
  • Is Add : Address[] = []; still reference to this.Add or it's variable defined just there? Commented Jan 28, 2019 at 11:32
  • ':' expected. Change this.Add.push({add:"102,ajaynagar", loc: 'andheri'}); Commented Jan 28, 2019 at 11:42

2 Answers 2

1

You missed an ":" in your push method:

this.Add.push({add:"102,ajaynagar", loc: "andheri"});
Sign up to request clarification or add additional context in comments.

Comments

1

Correct your code, you cant assign value like this, you missed ':'.

testAdd: Address[]= [{add: "D/102", loc: 'thane'},{add: "Room No, 106", loc: 'Mumbai'}];
Add : Address[] = [];
this.Add.push({add:"102,ajaynagar", loc: 'andheri'});
console.log(this.testAdd[0])  //results:-  {add: "D/102", loc: 'thane'}
console.log(this.Add[0]) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.