[英]Get random object from Object.freeze
如何編寫getRandomCounty()
function 以始終從 CountyCode 返回隨機countyCode
:
export class AppComponent {
countyCode: { [key: string]: string } = Object.freeze({
'01': 'Alba',
'02': 'Arad',
'03': 'Arges',
'04': 'Bacau',
'05': 'Bihor',
'06': 'Bistrita-Nasaud',
'07': 'Botosani',
'08': 'Brasov',
'09': 'Braila',
10: 'Buzau',
11: 'Caras-Severin',
12: 'Cluj',
13: 'Constanta',
14: 'Covasna',
15: 'Dambovita',
16: 'Dolj',
17: 'Galati',
18: 'Gorj',
19: 'Harghita',
20: 'Hunedoara',
21: 'Ialomita',
22: 'Iasi',
23: 'Ilfov',
24: 'Maramures',
25: 'Mehedinti',
26: 'Mures',
27: 'Neamt',
28: 'Olt',
29: 'Prahova',
30: 'Satu Mare',
31: 'Salaj',
32: 'Sibiu',
33: 'Suceava',
34: 'Teleorman',
35: 'Timis',
36: 'Tulcea',
37: 'Vaslui',
38: 'Valcea',
39: 'Vrancea',
40: 'Bucuresti',
41: 'Bucuresti S.1',
42: 'Bucuresti S.2',
43: 'Bucuresti S.3',
44: 'Bucuresti S.4',
45: 'Bucuresti S.5',
46: 'Bucuresti S.6',
51: 'Calarasi',
52: 'Giurgiu'
});
getRandomCounty() { // this must return random object from countyCode like {'03': 'Arges'}
// ?????
}
}
你可以這樣做: -
function getRandomCounty(obj) {
const keys = Object.keys(obj);
const randomeIndex = Math.floor(Math.random() * (keys.length));
return obj[keys[randomeIndex]];
}
class AppComponent { countyCode = Object.freeze({ '01': 'Alba', '02': 'Arad', '03': 'Arges', '04': 'Bacau', '05': 'Bihor', '06': 'Bistrita-Nasaud', '07': 'Botosani', '08': 'Brasov', '09': 'Braila', 10: 'Buzau', 11: 'Caras-Severin', 12: 'Cluj', 13: 'Constanta', 14: 'Covasna', 15: 'Dambovita', 16: 'Dolj', 17: 'Galati', 18: 'Gorj', 19: 'Harghita', 20: 'Hunedoara', 21: 'Ialomita', 22: 'Iasi', 23: 'Ilfov', 24: 'Maramures', 25: 'Mehedinti', 26: 'Mures', 27: 'Neamt', 28: 'Olt', 29: 'Prahova', 30: 'Satu Mare', 31: 'Salaj', 32: 'Sibiu', 33: 'Suceava', 34: 'Teleorman', 35: 'Timis', 36: 'Tulcea', 37: 'Vaslui', 38: 'Valcea', 39: 'Vrancea', 40: 'Bucuresti', 41: 'Bucuresti S.1', 42: 'Bucuresti S.2', 43: 'Bucuresti S.3', 44: 'Bucuresti S.4', 45: 'Bucuresti S.5', 46: 'Bucuresti S.6', 51: 'Calarasi', 52: 'Giurgiu' }); getRandomCounty() { // this must return random object from countyCode like {'03': 'Arges'} const props = Object.getOwnPropertyNames(this.countyCode); return this.countyCode[props[Math.floor(Math.random() * props.length)]]; } } const appComponent = new AppComponent(); console.log(appComponent.getRandomCounty());
我希望這對你有用
countyCode = [ {'01': 'Alba'}, {'02': 'Arad'}, {'03': 'Arges'}, {'04': 'Bacau'}, {'05': 'Bihor'}, {'06': 'Bistrita-Nasaud'}, {'07': 'Botosani'}, {'08': 'Brasov'}, {'09': 'Braila'}, {'10': 'Buzau'}, {'11': 'Caras-Severin'}, {'12': 'Cluj'}, {'13': 'Constanta'}, {'14': 'Covasna'}, {'15': 'Dambovita'}, {'16': 'Dolj'}, {'17': 'Galati'}, {'18': 'Gorj'}, {'19': 'Harghita'}, {'20': 'Hunedoara'}, {'21': 'Ialomita'}, {'22': 'Iasi'}, {'23': 'Ilfov'}, {'24': 'Maramures'}, {'25': 'Mehedinti'}, {'26': 'Mures'}, {'27': 'Neamt'}, {'28': 'Olt'}, {'29': 'Prahova'}, {'30': 'Satu Mare'}, {'31': 'Salaj'}, {'32': 'Sibiu'}, {'33': 'Suceava'}, {'34': 'Teleorman'}, {'35': 'Timis'}, {'36': 'Tulcea'}, {'37': 'Vaslui'}, {'38': 'Valcea'}, {'39': 'Vrancea'}, {'40': 'Bucuresti'}, {'41': 'Bucuresti S.1'}, {'42': 'Bucuresti S.2'}, {'43': 'Bucuresti S.3'}, {'44': 'Bucuresti S.4'}, {'45': 'Bucuresti S.5'}, {'46': 'Bucuresti S.6'}, {'51': 'Calarasi'}, {'52': 'Giurgiu'} ]; Object.freeze(countyCode); console.log(getRandomCounty()); function getRandomCounty() { // this must return random object from countyCode like {'03': 'Arges'} let keys = Object.keys(countyCode) return countyCode[keys[ keys.length * Math.random() << 0]]; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.