I assign the properties of an interface CountersData
as seen below. I use a flattenObject
to extract the most nested key and value data from responseDeploymentData
(as detailed One liner to flatten nested object)
This is clunky, so is there a more efficient/simple way to do this? See code below:
interface CountersData {
//more properties
count_targets?: number;
count_targets_excluded?: number;
count_targets_pending?: number;
count_targets_in_progress?: number;
count_targets_completed?: number;
count_targets_failed?: number;
}
// later in code
const countersData = {} as CountersData;
let targetData: any = flattenObject(responseDeploymentData);
countersData.count_targets = targetData.count_targets;
countersData.count_targets_excluded = targetData.count_targets_excluded;
countersData.count_targets_pending = targetData.count_targets_pending;
countersData.count_targets_in_progress = targetData.count_targets_in_progress;
countersData.count_targets_completed = targetData.count_targets_completed;
countersData.count_targets_failed = targetData.count_targets_failed;
Updated Fix:
['count_targets', 'count_targets_excluded'].forEach(key => countersData[key] = targetData[key]);