Arrays to export to excel

Why does when it is the following code:

		for (var i = 0; i < 3; i++) {
			this.arr.push({
				id: 2,
				firstName: "FirstName",
				lastName: "LastName",
				emailId: "lastname_firstname@yahoo.com",
				sex: "Female",
				isFilipino: true,
				maritalStatus: "Single"
			});
		};
```````````````````````````````````````````````````````````````````````
The following code this.arr3 return values:
````````````````````````````````````````````````````````````````````````````````````
		this.arr.forEach((a: any) => {
                      this.arr3.push(a);
		});
```````````````````````````````````````````````````````````````````````````````````````

But when in the following code:
```````````````````````````````````````````````````````````````````````````````````````
		this.employeeService.getEmployeesList().subscribe(data => {
		for (var i = 0; i < 3; i++) {
			this.arr.push({
				id: 2,
				firstName: "FirstName",
				lastName: "LastName",
				emailId: "lastname_firstname@yahoo.com",
				sex: "Female",
				isFilipino: true,
				maritalStatus: "Single"
			});
		}
		}
		);

The following code this.arr3 return as empty:

		this.arr.forEach((a: any) => {
                      this.arr3.push(a);
		});
``````````````````````````````````````````````````````````````````````````````````````````
Thanks in advance.

if you wanna equalize two arrays just need this way:

let arr3 = [] // first define an empty array
[... arr3] = [...arr] //then you can push all of arr into arr3

But the code works outside the subscribe but when it come from inside the subscribe it doesn’t work.

cause “this” keyword refer to this.employeeService not the class contains arr and arr3
you can use arr and arr3 as input of subscribe method

Hi,

can you modify the code so that it would work?

Thanks.

ease you’ve defined a function named subscribe that just accept one input (data)
you must define it like this:

subscribe(arr , data){
        for (var i = 0; i < 3; i++) {
			arr.push({
				id: 2,
				firstName: "FirstName",
				lastName: "LastName",
				emailId: "lastname_firstname@yahoo.com",
				sex: "Female",
				isFilipino: true,
				maritalStatus: "Single"
			});
       return arr
    }

then you can do what you wanna do with arr ;

in your code this keyword cant access to arr