[英]data structure for key value angular 5 typescript
I'm looking for a solution that can replicate a key/value dictionary in Python. 我正在寻找一种可以在Python中复制键/值字典的解决方案。 I am using Angular 5 typescript for my project and I am new to the datatypes available.
我在我的项目中使用Angular 5打字稿,并且对可用的数据类型不熟悉。 I have read a bunch of things online and it seems that because Angular is always changing, there are datatypes that are being depreciated or not supported anymore.
我在网上阅读了很多东西,似乎因为Angular总是在变化,所以有些数据类型已被折旧或不再受支持。
This is what I am trying to accomplish. 这就是我想要完成的。 If anyone can recommend a good resource to look at, I would greatly appreciate that.
如果有人可以推荐一个不错的资源供参考,我将不胜感激。
Python equivalent 相当于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)
Your code can be written in TypeScript as below: 您的代码可以用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]);
}
});
You can use ES6 " Map " data type: 您可以使用ES6“ Map ”数据类型:
Have a look at the article https://medium.com/front-end-hacking/es6-map-vs-object-what-and-when-b80621932373 . 看一下文章https://medium.com/front-end-hacking/es6-map-vs-object-what-and-when-b80621932373 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.