簡體   English   中英

使用 React 基於嵌套值創建新的對象數組

[英]Create new array of objects based off nested value using React

我從 API 返回一個對象數組,我想在 useEffect 中創建一個新變量,其單位設置為isActive 不確定最好的方法。

[
  {
    "fields": {
      "assetKey": {
        "en-US": "YMW-24"
      },
      "isActive": {
        "en-US": false
      }
    }
  },
  {
    "fields": {
      "assetKey": {
        "en-US": "YMW-25"
      },
      "isActive": {
        "en-US": true
      }
    }
  },
  {
    "fields": {
      "assetKey": {
        "en-US": "YMW-21"
      },
      "isActive": {
        "en-US": false
      }
    }
  }
]

與 promise 一起使用效果

useEffect(() => {
    const referenceEntityIDs = unitsField.map((i) => i.sys.id);
    Promise.all(referenceEntityIDs.map((id) => sdk.space.getEntry(id)))
    .then((data) => {
        // Getting all units  
        const getAllUnits = data.map((unit) => unit.fields.assetKey["en-US"]);
        setAllUnits(getAllUnits);

        // TODO Return units that are set to active in a variable

    })
    .catch(() => {
        console.log("Units fail :(")
    });
}, []);
useEffect(() => {
    const referenceEntityIDs = unitsField.map((i) => i.sys.id);
    Promise.all(referenceEntityIDs.map((id) => sdk.space.getEntry(id)))
    .then((data) => {
        // Getting all units  
        const getAllUnits = data.map((unit) => unit.fields.assetKey["en-US"]);
        setAllUnits(getAllUnits);

        // TODO Return units that are set to active in a variable
        const active = getAllUnits.filter((unit) => unit.fields.isActive["en-US"]);

    })
    .catch(() => {
        console.log("Units fail :(")
    });
}, []);

暫無
暫無

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

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