I'm trying to figure out how to alert a JavaScript object based on the URL string. The URL will have a URL string that includes something like this: ?cat=2
I have the following object created:
var cat = (new URL(location)).searchParams.get('cat')
var CAT_LOOKUP = {
1: {
category: "Fire",
idAttr: "fire",
name: "fire"
},
2: {
category: "Medical Staff",
idAttr: "medicalStaff",
name: "medicalStaff"
},
3: {
category: "Physician",
idAttr: "physician",
name: "physician"
},
4: {
category: "Police",
idAttr: "police",
name: "police"
}
}
I have this started to display the alert, but am missing something:
function alertCat(obj){
for(var key in obj) {
alert('key: ' + key + '\n' + 'value: ' + obj[key]);
if( typeof obj[key] === 'object' ) {
alertObject(obj[key]);
}
}
}
alertCat(CAT_LOOKUP[cat]);
I need access to the object for use in a form on the page.
I figured it out:
$( document ).ready(function() {
var cat = (new URL(location)).searchParams.get('cat')
alert('T = ' + cat);
var CAT_LOOKUP = {
1: {
category: "Fire",
idAttr: "fire",
name: "fire"
},
2: {
category: "Medical Staff",
idAttr: "medicalStaff",
name: "medicalStaff"
},
3: {
category: "Physician",
idAttr: "physician",
name: "physician"
},
4: {
category: "Police",
idAttr: "police",
name: "police"
}
}
alert(CAT_LOOKUP[cat].category);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.