键值 angular 5 打字稿的数据结构

data structure for key value angular 5 typescript

我正在寻找可以在 Python 中复制 key/value 字典的解决方案。我正在为我的项目使用 Angular 5 打字稿,我对可用的数据类型不熟悉。我在网上读了很多东西,似乎因为 Angular 总是在变化,所以有些数据类型正在贬值或不再受支持。

这就是我想要完成的。如果有人能推荐一个好的资源看看,我将不胜感激。

Python相当于

string = "the bump from the sump in the sump for the trump and the bump"


a = {}
for x in string.split():
    if x in a.keys():
        a[x] += 1
    else:
        a[x] = 1
print(a)

for b in a.keys():
    if a[b] > 1 and a[b] < 4:
        print(b)
    if a[b] >= 4:
        print(b)

您可以使用 ES6 "Map" 数据类型:

看看这篇文章 https://medium.com/front-end-hacking/es6-map-vs-object-what-and-when-b80621932373

您的代码可以用 TypeScript 编写如下:

let string = "the bump from the sump in the sump for the trump and the bump";

let words = string.split(' ');

let wordCount = words.reduce(((acc, elem) => { 
  acc[elem] = (acc[elem] || 0) + 1; return acc;
}), {});

Object.keys(wordCount).forEach((key) => {
  if (wordCount[key] > 1 && wordCount[key] < 4) {
    console.log(wordCount[key]);
  } else if (wordCount[key] >= 4) {
    console.log(wordCount[key]);
  }
});