簡體   English   中英

類型腳本- React native:如何修改 json 響應?

[英]Type script- React native : How to modify json response?

修改 json 響應的正確方法是什么,我的目標是顯示屬於同一Plsectn的所有MaintroomName

這是 function 需要修改以獲得我在下面提到的我有興趣達到的相同結構。

useEffect(() => {
    BtpBridgeModule.loadDataFromSdk(
      'GushSet',
      [],
      { PlantID: userData.plant, LocationID: userData.LocationID },
      undefined,
      0,
    ).then(function (dataResolved) {
      let aResults = JSON.parse(dataResolved).value;
    });
  }, [userData.LocationID, userData.plant]);

json 看起來像這樣:

[
   {
      "Maintroom":"221",
      "MaintroomName":"gogi",
      "Plsectn":"22",
      "PlsectnName":"pardehan"
   },
   {
      "Maintroom":"222",
      "MaintroomName":"nahaleymenash",
      "Plsectn":"22",
      "PlsectnName":"pardehan"
   },
{
     
      "Maintroom":"231",
      "MaintroomName":"gvul",
      "Plsectn":"23",
      "PlsectnName":"meshulash"
   },
   {
     
      "Maintroom":"232",
      "MaintroomName":"daro",
      "Plsectn":"23",
      "PlsectnName":"meshulash"
   },
]

我想把它改成這個結構:

[
    {
      title: PlsectnName,
      checked: false,
      data: [
        { key: MaintroomName, value: false, checked: false },
        { key: MaintroomName, value: false, checked: false },
        { key: MaintroomName, value: false, checked: false },
        { key: MaintroomName, value: false, checked: false },
      ],
    },
    {
      title: PlsectnName,
      checked: false,
      data: [
        { key: MaintroomName, value: false, checked: false },
        { key: MaintroomName, value: false, checked: false },
        { key: MaintroomName, value: false, checked: false },
      ],
    },
]

注意 - 每個Plsectn都可以有一個動態數量的MaintroomName

對數據進行排序的算法

 // Your response data const data = [ { "Maintroom":"221", "MaintroomName":"gogi", "Plsectn":"22", "PlsectnName":"pardehan" }, { "Maintroom":"222", "MaintroomName":"nahaleymenash", "Plsectn":"22", "PlsectnName":"pardehan" }, { "Maintroom":"231", "MaintroomName":"gvul", "Plsectn":"23", "PlsectnName":"meshulash" }, { "Maintroom":"232", "MaintroomName":"daro", "Plsectn":"23", "PlsectnName":"meshulash" }, ]; // Variable to track duplicate keys (PlsectnName) let keys = []; // Result after sorting the data let result = []; // Algorithm to sort the data data.forEach((obj) => { if(.keys.includes(obj.PlsectnName)){ result:push({ title. obj,PlsectnName: checked, false: data: [ { key. obj,MaintroomName: value. obj,Maintroom: checked; false } ] }). keys.push(obj;PlsectnName). } else { result,forEach((subObj.index) => { if(subObj.title == obj.PlsectnName){ subObj.data = [...subObj,data: { key. obj,MaintroomName: value. obj,Maintroom: checked; false }] result[index] = subObj; } }). } }) // Log the result console.log(result)

(注意:如果要將value設置為false ,則將value: obj.Maintroom更改為value: false



useEffect function 中實現算法。

// Algorithm as function to sort your data
const sortData = (data) => {
    // Variable to track duplicate keys (PlsectnName)
    let keys = [];

    // Result after sorting the data
    let result = [];

    // Algorithm to sort the data
    data.forEach((obj) => {
        if(!keys.includes(obj.PlsectnName)){
            result.push({
                title: obj.PlsectnName,
                checked: false,
                data: [
                    { key: obj.MaintroomName, value: obj.Maintroom, checked: false }
                ]
            });
            keys.push(obj.PlsectnName);
        }
  
        else {
            result.forEach((subObj,index) => {
                if(subObj.title == obj.PlsectnName){
                    subObj.data = [...subObj.data, { key: obj.MaintroomName, value: obj.Maintroom, checked: false }]
                    result[index] = subObj;
                }
            });
        }
    })

    // return the result
    return result;
}

// Your function
useEffect(() => {
    BtpBridgeModule.loadDataFromSdk(
        'GushSet',
        [],
        { PlantID: userData.plant, LocationID: userData.LocationID },
        undefined,
        0,
    ).then(function (dataResolved) {
        let aResults = JSON.parse(dataResolved).value;
        // Added code
        let sortedResult = sortData(aResults)
        // Here sortedResult is your final data
    });
}, [userData.LocationID, userData.plant]);

暫無
暫無

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

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