My TypeScript service is responsible for populating an associative array:
fun populateData(){
let tempArr;
tempArr = [];
this.service.get('Post', 1, 'true').subscribe(
(response) => {
this.loadingIcon = false;
for (let i = 0; i < response.results.length; i++) {
tempList = response.results[i]['tags'];
for ( let iter of tempList){
if ( iter in tempArr) {
tempArr[iter] = tempArr[iter] + 1;
}else {
tempArr[iter] = 1;
}
}
}
},
(error) => {
if (error['status'] === 401) {
localStorage.clear();
this.router.navigateByUrl('/login');
} else {
this.router.navigateByUrl('/error');
}
}
);
console.log(tempArr);
/*
This function is inside a class, once I iterate get access to tempArr I will be assigning the tempArr data to a class variable like
for (items in tempArr){
this.data.push(items, tempArr[items]);
}
*/
}
Upon execution, the console displays the populated associative array:
https://i.sstatic.net/lqNzG.png
Despite the successful population, I encountered difficulty iterating through the array. I attempted various methods, such as:
for ( const key in tempArr) {
console.log(key + ':' + tempArr[key]);
}
I am seeking a solution to retrieve both keys and values from the array.