解构具有相同名称的内部对象 属性

destructuring inner objects with same name property

大家好,我有下面的对象结构,我正在尝试使用解构技术获取所有内部对象的名称,但无法做到这一点,下面是对象结构

   {
       massingType {
            id
            name
        }
        ashraeClimateZone {
             id
            name
        }
        sourceOfData {
             id
             name    
        }
        .....
    } 

我正在像下面那样进行解构

 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';

任何人都可以告诉我如何实现这个解决方案,在此先感谢!!

使用解构赋值,您可以重命名任何解构变量。您目前正在将每个“根”键重命名为 name,这会导致重复声明,但您似乎真的想访问和解构每个嵌套的 name 属性。

const {
  sourceOfData: {
    name: sourceOfDataName,
  },
  massingType: {
    name: massingTypeName,
  },
  ashraeClimateZone: {
    name: ashraeClimateZoneName,
  },
} = item;

考虑到只是第一个解构值,上面

  1. item
  2. 解构 sourceOfData
  3. 将其重新分配给另一个对象
  4. name属性解构为
  5. 重新分配给 sourceOfDataName