I have a two json objects. i need to merge those in a single object which has same id.
Example
const [orderdetails, setOrderdetails] = useState([]);
const [ordertracking, setOrdertracking] = useState([]);
useEffect(() => {
fetch('first api')
.then((response) => response.json())
.then((json) => setOrderdetails(json))
.catch((error) => console.error(error))
.finally(() => setLoading(false));
}, []);
useEffect(() => {
fetch('second api')
.then((response) => response.json())
.then((json) => setOrdertracking(json))
.catch((error) => console.error(error))
.finally(() => setLoading(false));
}, []);
Sample data
First api response in orderdetails
{
"statusCode": "200",
"message": "success",
"content": [
{ subject_id: 711, topics: ["Test", "Test2"] },
{ subject_id: 712, topics: ["topic1", "Topic2"] }
]
}
Second api response in ordertracking
{
"statusCode": "200",
"message": "success",
"content": [
{subject_id: 711, subject_name: "Science"},
{subject_id: 712, subject_name: "Maths"}
]
}
I want the merged result to be:
const result = [
{
subject_id: "711",
subjectName: "Science",
topics: [ "Test", "Test2" ]
},
{
subject_id: "712",
subjectName: "Maths",
topics: [ "topic1", "Topic2" ]
}
];
As you can see there are two api response in a josn format I need to merge those json object which is inside a content which has same 'subject_id' and rest of the values as a single data.
I need a solution in javascript for my react native app if you can suggest me a react native code