以下は、配列内で検出されるいくつかの文字列で一致を検索するために、以下の関数で使用される正規表現を含むオブジェクトです。
一致する場合は、その一致が自動車事故(つまり、オブジェクトの最後の要素の一致)であるかどうかを確認することです。
27行目でそれを実行しようとしましたが、INCIDENT_MATCHES [type]が要素の名前(trafficAccidents)ではなく、オブジェクト要素のコンテンツ(つまり正規表現)を参照しているため、機能しません。
コンテンツの代わりに要素名を参照するために何を書くのですか?
1. var INCIDENT_MATCHES = {
2. battery: /\w*(bråk)\w*|överfall|slagsmål|slogs|misshandel|misshandlad|\w*(tjuv)\w*/ig,
3. burglaries: /snattade|snattare|snatta|inbrott|bestulen|stöld|\w*(tjuv)\w*/ig,
4. robberies: /\w*(rån)\w*|personrån|\w*(ryckning)\w*|väskryckt*/ig,
5. gunfire: /skottlossning|skjuten|sköt/ig,
6. drugs: /narkotikabrott/ig,
7. vandalism: /skadegörelse|klotter|\w*(klottra)\w*/ig,
8. trafficAccidents: /(trafik|bil)olycka|(trafik|bil)olyckor|\w*(personbil)\w*|singelolycka|kollision|\w*(kollidera)\w*|påkörd|trafik|smitningsolycka/ig,
9. };
10.
11. function FindIncidents(incidentReports) {
12.
13. var incidentCounts = {};
14. var incidentTypes = Object.keys(INCIDENT_MATCHES);
15. incidentReports[2].forEach(function(incident) {
16. matchFound = false;
17.
18. incidentTypes.forEach(function(type) {
19. if(typeof incidentCounts[type] === 'undefined') {
20. incidentCounts[type] = 0;
21. }
22. var matchFound = incident.match(INCIDENT_MATCHES[type]);
23. if(matchFound){
24. matchFound = true;
25. incidentCounts[type] += 1;
26. console.log(INCIDENT_MATCHES[type]); // refers to the elements content, not the name which I want.
27. if (INCIDENT_MATCHES[type] == 'trafficAccidents') {
28. //
29. }
30 }
31. });
32. });
33. }
34.
35. FindIncidents(arrayWithStringsToMatch);