Within my Angular 2 component, I am utilizing an array named fieldlist which is populated by data retrieved from an http.get request. The array is declared as follows:
fieldlist: string[] = [];
I populate this array by iterating through the JSON response obtained from the http.get request.
this.http.get(getform_endpoint,requestOptions).map((res:
Response) => res.json()).subscribe(
res => {
this.FormData = res.schema;
res.fields.forEach(element => {
this.fieldlist.push(element);
});
});
In a separate function, I attempt to combine the elements of fieldlist into a single string using the join() method:
create_hidden_qp() {
let elementsnamevalue = this.fieldlist.join();
console.log("hello", this.fieldlist.join());
}
However, when I convert the array to a string in this manner, it returns an empty response. On the other hand, when I log the array directly, the elements are displayed correctly:
console.log("hello", this.fieldlist);
The output shows the array contents as expected:
hello[] 0 :"userroleid" 1: "ruletype" 2: "employeeid"
Where could I be going wrong?
A) Incorrect declaration? b) Improper assignment? c) Incorrect access to array elements?