From 6ae25efd519a995a83ffd0a23c067cb93aebd501 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Mon, 6 May 2019 12:01:31 +0200 Subject: [PATCH] MOBILE-3008 core: Filter undefined in objectToArrayOfObjects --- src/core/course/pages/section/section.ts | 2 +- src/providers/utils/utils.ts | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/core/course/pages/section/section.ts b/src/core/course/pages/section/section.ts index 79ab769cf..c1dada99d 100644 --- a/src/core/course/pages/section/section.ts +++ b/src/core/course/pages/section/section.ts @@ -52,7 +52,7 @@ export class CoreCourseSectionPage implements OnDestroy { courseHandlers: CoreCourseOptionsHandlerToDisplay[]; courseMenuHandlers: CoreCourseOptionsMenuHandlerToDisplay[] = []; dataLoaded: boolean; - downloadEnabled: boolean; + downloadEnabled = false; downloadEnabledIcon = 'square-outline'; // Disabled by default. prefetchCourseData = { prefetchCourseIcon: 'spinner', diff --git a/src/providers/utils/utils.ts b/src/providers/utils/utils.ts index 8a45fa2d2..2276eaf85 100644 --- a/src/providers/utils/utils.ts +++ b/src/providers/utils/utils.ts @@ -977,14 +977,21 @@ export class CoreUtilsProvider { objectToArrayOfObjects(obj: object, keyName: string, valueName: string, sortByKey?: boolean, sortByValue?: boolean): any[] { // Get the entries from an object or primitive value. 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. const keys = Object.keys(value); let entries = []; keys.forEach((key) => { - const newElKey = elKey ? elKey + '[' + key + ']' : key; - entries = entries.concat(getEntries(newElKey, value[key])); + const newElKey = elKey ? elKey + '[' + key + ']' : key, + subEntries = getEntries(newElKey, value[key]); + + if (subEntries) { + entries = entries.concat(subEntries); + } }); return entries;