I have the following code:
switch (entityType) {
case "Exam":
entityId = formData.examId;
idColumn = 'examId';
break;
case "Subject":
entityId = formData.subjectId;
idColumn = 'subjectId';
break;
}
It's repeated more times than this but I am just showing two case values here. I believe it's possible to get the values for idColumn by getting the lowercase of entityType and adding Id. But is there a way I could extract the value in formData for the field based on entityType without having to manually code this many times.
You can replace the whole with
idColumn = entityType.toLowerCase()+'Id';
entityId = formData[idColumn];
I'd suggest you to read MDN's Working with Objects .
你可以得到它:
entityId = formData[entityType.toLowerCase()+'Id'];
Of course there is! You can use an object just like an array:
idColumn = entityType.toLowerCase() + "Id";
entityId = formData[idColumn];
You can prepare a map and then access the fields directly.
Example:
var map = {
'Exam': 'examId',
'Subject': 'subjectId'
}
if (map[entityType]) {
entityId = formData[map[entityType]];
idColumn = map[entityType];
}
else {
// error handling
}
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.