Merge pull request #1895 from dpalou/MOBILE-3008

MOBILE-3008 core: Filter undefined in objectToArrayOfObjects
main
Juan Leyva 2019-05-07 20:46:24 +02:00 committed by GitHub
commit 9b4da3e474
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 4 deletions

View File

@ -52,7 +52,7 @@ export class CoreCourseSectionPage implements OnDestroy {
courseHandlers: CoreCourseOptionsHandlerToDisplay[]; courseHandlers: CoreCourseOptionsHandlerToDisplay[];
courseMenuHandlers: CoreCourseOptionsMenuHandlerToDisplay[] = []; courseMenuHandlers: CoreCourseOptionsMenuHandlerToDisplay[] = [];
dataLoaded: boolean; dataLoaded: boolean;
downloadEnabled: boolean; downloadEnabled = false;
downloadEnabledIcon = 'square-outline'; // Disabled by default. downloadEnabledIcon = 'square-outline'; // Disabled by default.
prefetchCourseData = { prefetchCourseData = {
prefetchCourseIcon: 'spinner', prefetchCourseIcon: 'spinner',

View File

@ -977,14 +977,21 @@ export class CoreUtilsProvider {
objectToArrayOfObjects(obj: object, keyName: string, valueName: string, sortByKey?: boolean, sortByValue?: boolean): any[] { objectToArrayOfObjects(obj: object, keyName: string, valueName: string, sortByKey?: boolean, sortByValue?: boolean): any[] {
// Get the entries from an object or primitive value. // Get the entries from an object or primitive value.
const getEntries = (elKey, value): any[] | any => { const getEntries = (elKey, value): any[] | any => {
if (typeof value == 'object') { if (typeof value == 'undefined' || value == null) {
// Filter undefined and null values.
return;
} else if (typeof value == 'object') {
// It's an object, return at least an entry for each property. // It's an object, return at least an entry for each property.
const keys = Object.keys(value); const keys = Object.keys(value);
let entries = []; let entries = [];
keys.forEach((key) => { keys.forEach((key) => {
const newElKey = elKey ? elKey + '[' + key + ']' : key; const newElKey = elKey ? elKey + '[' + key + ']' : key,
entries = entries.concat(getEntries(newElKey, value[key])); subEntries = getEntries(newElKey, value[key]);
if (subEntries) {
entries = entries.concat(subEntries);
}
}); });
return entries; return entries;