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

时间:2018-07-19 02:08:39

标签: angular typescript angular5 angular-datatables

我正在寻找一种可以在Python中复制键/值字典的解决方案。我在我的项目中使用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)

2 个答案:

答案 0 :(得分:1)

您的代码可以用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]);
  }
});

答案 1 :(得分:-1)

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

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