Arrays TypeScript无法迭代关联数组

Arrays TypeScript无法迭代关联数组,arrays,typescript,Arrays,Typescript,我有一个用typescript填充关联数组的服务 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++) {

我有一个用typescript填充关联数组的服务

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]);
       }
        */
}

我希望它们都是数组中的键和值

TypeScript通常假定数组的键是数字。你所做的可能有用,但不是很地道。我不打算重写整个函数,但这里有几个要点:

在构造关联数组(从现在起简称为map)时,应尝试使用对象而不是数组:

const tagCounts: { [key: string]: number } = {};
for (const result of response.results) {
    for (const tag of result.tags) {
        tagCounts[tag] = (tagCounts[tag] || 0) + 1;
    }
}
然后,您可以使用以下命令迭代结果:

for (const tag of Object.keys(tagCounts)) {
    const count = tagCounts[tag];
    // Other stuff here
}
或者,如果您有
对象的多边形填充。条目
,则使用:

for (const [tag, count] of Object.entries(tagCounts)) {
    // Other stuff here
}

查看您的代码,
this.data.push
似乎也是错误的:它将向
数据
数组中添加一个字符串和一个数字,这几乎肯定不是您想要的。如果您想存储键值对,那么您可能需要考虑将<代码>数据< /代码>转换为对象。

类型脚本通常假定数组的关键字是数字。你所做的可能有用,但不是很地道。我不打算重写整个函数,但这里有几个要点:

在构造关联数组(从现在起简称为map)时,应尝试使用对象而不是数组:

const tagCounts: { [key: string]: number } = {};
for (const result of response.results) {
    for (const tag of result.tags) {
        tagCounts[tag] = (tagCounts[tag] || 0) + 1;
    }
}
然后,您可以使用以下命令迭代结果:

for (const tag of Object.keys(tagCounts)) {
    const count = tagCounts[tag];
    // Other stuff here
}
或者,如果您有
对象的多边形填充。条目
,则使用:

for (const [tag, count] of Object.entries(tagCounts)) {
    // Other stuff here
}

查看您的代码,
this.data.push
似乎也是错误的:它将向
数据
数组中添加一个字符串和一个数字,这几乎肯定不是您想要的。如果您想存储键值对,您可能需要考虑将<代码>数据< /代码>转换为对象。

是否有理由不使用对象?它们正是为了这个目的而设计的。@Gustorn我不知道如何使用object来实现这个目的,任何指针都会有很大帮助。我必须看看
tempArr
是如何实现的constructed@Gustorn我已经用tempArr声明更新了这个问题,您使用的不是
array
,而是plain对象,而是遍历所有对(关键、价值)您可以使用ES7
Object.entries
方法。您不使用对象有什么原因吗?它们就是为了这个目的而制作的。@Gustorn我不知道如何使用对象来实现这一点,任何指针都会有很大帮助。我必须看看
tempArr
是如何使用的constructed@Gustorn我已经用tempArr声明更新了这个问题不使用
array
而是使用普通对象,要遍历所有对(键、值),可以使用ES7
object.entries
方法。不过,我在尝试使用object.keys(标记计数)进行迭代时尝试了这一方法它没有进入for循环。我可以在控制台日志中看到tagCounts对象中的数据,我厌倦了打印object.keys(tagCounts),它正在打印一个大小为零的空数组。但是,我在尝试使用object.keys(tagCounts)进行迭代时尝试了此方法它没有进入for循环。我可以在控制台日志中看到tagCounts对象中的数据,我厌倦了打印object.keys(tagCounts),它正在打印大小为零的空数组。