javascript 键入脚本-React Native:如何修改json响应?

cgvd09ve  于 2022-11-27  发布在  Java
关注(0)|答案(1)|浏览(126)

修改json响应的正确方法是什么?我的目标是显示属于同一个Plsectn的所有MaintroomName

这是需要修改的函数,以获得我在下面提到的我感兴趣的结构。

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

vawmfj5a

vawmfj5a1#

对数据进行排序的算法

// 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)

(Note:如果要将***value设置为false***,请将***value: obj.Maintroom更改为value: false***)

在***useEffect***函数中实现算法。

// 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]);

相关问题