[英]Type script- React native : How to modify json response?
What is the correct way to modify json response, My goal is to display all the MaintroomName
belonging to the same Plsectn
修改 json 响应的正确方法是什么,我的目标是显示属于同一
Plsectn
的所有MaintroomName
This is the function that needs to modify to get the same structure which I mentioned below that I am interested in reaching.这是 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]);
The json look like this: 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"
},
]
I wanna change it to this structure:我想把它改成这个结构:
[
{
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 },
],
},
]
Note - each Plsectn
can have a dynamic number of MaintroomName
.注意 - 每个
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)
(Note: If you want to set the value
as false
then change value: obj.Maintroom
to value: false
) (注意:如果要将
value
设置为false
,则将value: obj.Maintroom
更改为value: false
)
useEffect
function.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.