簡體   English   中英

如果所有 id 都未定義,則將不同的 id 推送到數組並獲取一個數組

[英]pushing different ids to array and getting an array if all id's are undefined

我正在嘗試將 id 添加到數組中,如果它們可用,如下所示

export const extractOpaqueConstructionType = library => {
  const opaqueConstructionSecondaryIds= [];
  opaqueConstructionSecondaryIds.push(library?.exteriorWallId);
  opaqueConstructionSecondaryIds.push(library?.exteriorFloorId);
  opaqueConstructionSecondaryIds.push(library?.roofId);
  opaqueConstructionSecondaryIds.push(library?.interiorWallId);
  opaqueConstructionSecondaryIds.push(library?.interiorFloorId);
  opaqueConstructionSecondaryIds.push(library?.belowGradeWallId);
  opaqueConstructionSecondaryIds.push(library?.slabOnGradeId);
  return { opaqueConstructions: opaqueConstructionSecondaryIds || null };
};

我在其他地方調用 function 上方,如下所示

extractSecondaryIds: library => {
    const secondaryIds = {
      ...extractSourceOfData(library),
      ...extractOpaqueConstructionType(library), // this is where i am calling above function
      ...extractGlazingConstructionType(library)
    };
    return secondaryIds;
  },

if all the above id's for example(exteriorWallId, exteriorFloorId, etc...) are undefined, I am getting output from the above function ( extractOpaqueConstructionType ) like opaqueConstructions: [null] where as I am expecting like this opaqueConstructions: null if id's are不明確的

任何人都可以就此提出任何想法或替代方法,非常感謝我,非常感謝。

嘗試如下

export const extractOpaqueConstructionType = library => {
const opaqueConstructionSecondaryIds= [
library?.exteriorWallId
library?.exteriorFloorId,
library?.roofId,
library?.interiorWallId,
library?.interiorFloorId,
library?.belowGradeWallId,
library?.slabOnGradeId,
].filter(keyValue => keyValue!==undefine);

return { opaqueConstructions:opaqueConstructionSecondaryIds.length? 
opaqueConstructionSecondaryIds : null };
};

它需要在推送所有值后過濾數組。

您還可以通過簡單地刪除推送調用來簡化代碼

export const extractOpaqueConstructionType = library => {
  const opaqueConstructions = [
    library?.exteriorWallId,
    library?.exteriorFloorId,
    library?.roofId,
    library?.interiorWallId,
    library?.interiorFloorId,
    library?.belowGradeWallId,
    library?.slabOnGradeId,
  ].filter(it => it !== null);

  return { opaqueConstructions: opaqueConstructions.length ? opaqueConstructions : null };
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM