So this is the api response i'am getting.
[
{
"Declaration":[
{
"id":111,
"emp_name":"john",
"pan":"QWHTERT",
"desigination":"SDE",
"house_loan":0,
"car_loan":0
}
],
"Rent":[
{
"house_rent":12000,
"gym_rent":1500
}
]
},
{
"Declaration":[
{
"id":112,
"emp_name":"emy",
"pan":"QWHHGTRT",
"desigination":"HR",
"house_loan":10000,
"car_loan":2000
}
],
"Rent":[
{
"house_rent":12000,
"gym_rent":1500
}
]
},
{
"Declaration":[
{
"id":114,
"emp_name":"diya",
"pan":"QWHHGTRT",
"desigination":"PM",
"house_loan":30000,
"car_loan":6000
}
],
"Rent":[]
},
{
"Declaration":[
{
"id":115,
"emp_name":"ringu",
"pan":"AWSDWHHGTRT",
"desigination":"TL",
"house_loan":90000,
"car_loan":7000
}
],
"Rent":[
{
"house_rent":12000,
"gym_rent":1500
}
]
},
{
"Declaration":[
{
"id":116,
"emp_name":"jincy",
"pan":"LPOWHHGTRT",
"desigination":"SDE 2",
"house_loan":80000,
"car_loan":4000
}
],
"Rent":[]
}
]
i need to convert this response into single array of objects. where Rent array should merge with declaration array. and it should be a single array of object with all the data of rent and declaration. the desired out should look like shown below.
{
declaration:[
{
"id":115,
"emp_name":"ringu",
"pan":"AWSDWHHGTRT",
"desigination":"TL",
"house_loan":90000,
"car_loan":7000,
"house_rent":12000,
"gym_rent":1500
},
{
"id":111,
"emp_name":"john",
"pan":"QWHTERT",
"desigination":"SDE",
"house_loan":0,
"car_loan":0,
"house_rent":12000,
"gym_rent":1500
},
{
"id":112,
"emp_name":"emy",
"pan":"QWHHGTRT",
"desigination":"HR",
"house_loan":10000,
"car_loan":2000,
"house_rent":12000,
"gym_rent":1500
}
]
}
So it will be easy to list these inside a grid.
I have tried my level best. attaching the code that i have tried that didn't work
class App extends React.Component{
constructor() {
super();
this.state({
userDetails:[]
})
}
componentDidMount(){
renderData();
}
renderData(){
instance
.get("/rentdeails/user")
.then((res)=>{
const declarationDetails = [];
const rentDetails = [];
for(let key in res.data){
declarationDetails = res.data[key].Declaration
rentDetails = res.data[key].Rent
}
const mergedArray = [...declarationDetails, ...rentDetails];
this.setState({userDetails:mergedArray});
console.log(mergedArray);
})
}
Feel free to ask any queries. Any help will be appreciated.