小编kum*_*ain的帖子

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

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

   {
       massingType {
            id
            name
        }
        ashraeClimateZone {
             id
            name
        }
        sourceOfData {
             id
             name    
        }
        .....
    } 
Run Code Online (Sandbox Code Playgroud)

我正在做如下的解构

 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 …
Run Code Online (Sandbox Code Playgroud)

javascript object destructuring ecmascript-6 reactjs

2
推荐指数
1
解决办法
1730
查看次数