hi all I have below object structure that i am trying to get the name of all inner objects using destructuring technique but could not be able to do that, Below is the object structure
{
massingType {
id
name
}
ashraeClimateZone {
id
name
}
sourceOfData {
id
name
}
.....
}
and i am doing the destrcturing like as below
constructionSetData.constructionSets.forEach(item => {
if (
item.ashraeClimateZone?.id === ashraeClimateZoneId &&
item.massingType?.id === massingTypeId &&
item.sourceOfData?.id === energyCodeId
) {
matchedConstructionDataSet.push(item.name);
const { sourceOfData: name, massingType: name, ashraeClimateZone: name } = item; // getting error here Identifier 'name' has already been declared
}
});
return matchedConstructionDataSet.length
? `${matchedConstructionDataSet.join(', ')}` // here i need to use above names coming from three inner objects
: 'No construction set found with the current criteria';
Could any one please let me know how can i achieve this solution, many thanks in advance!!
Using destructuring assignment you can rename any destructured variables. You are currently renaming each "root" key as name which is causing duplicate declarations, but it appears you really want to access & destructure the nested name property of each.
const {
sourceOfData: {
name: sourceOfDataName,
},
massingType: {
name: massingTypeName,
},
ashraeClimateZone: {
name: ashraeClimateZoneName,
},
} = item;
Considering just the first destructured value, the above
sourceOfData from itemname property to sourceOfDataName.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With