Files
threshold-ui/addons/api/addon/utils/flatten-nested-object.js
T
55e1e68f30 [COMPLIANCE] Update Copyright and License Headers (#3099)
Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
Co-authored-by: Dharini <dharini@hashicorp.com>
2026-01-20 11:35:10 -05:00

28 lines
778 B
JavaScript

/**
* Copyright IBM Corp. 2021, 2025
* SPDX-License-Identifier: BUSL-1.1
*/
import { typeOf } from '@ember/utils';
/**
* Flattens a nested object into a single-level object.
* @param obj
* @param result
* @param parentKey
* @returns {Object}
*/
export const flattenObject = (obj, result = {}, parentKey = '') => {
for (const key in obj) {
// Check if the value is an object and it is not null
if (typeOf(obj[key]) === 'object' && obj[key] !== null) {
// Recursively flatten the object
flattenObject(obj[key], result, `${parentKey}${key}.`);
} else if (obj[key] !== null && obj[key] !== '') {
// Only add to result if the value is not null or an empty string
result[`${parentKey}${key}`] = obj[key];
}
}
return result;
};