Currently, I have an Object which is having two keys ie data
and desc_coa
and I want to merge the Both key and get the Final result
{
"data": [
{
"5-1001": 1,
"sum_kredit": 0,
"sum_debit": 100000,
"total_items": 1
},
{
"1-1101": 1,
"sum_kredit": 100000,
"sum_debit": 0,
"total_items": 1
}
],
"desc_coa": [
{
"coa": "5-1001",
"keterangan": "HPP"
},
{
"coa": "1-1101",
"keterangan": "Kas"
}
]
}
Expected Output want to merge them all into a new object, the results I expect to like this.
{
"data": [
{
"5-1001": 1,
"sum_kredit": 0,
"sum_debit": 100000,
"total_items": 1,
"coa": "5-1001",
"keterangan": "HPP"
},
{
"1-1101": 1,
"sum_kredit": 100000,
"sum_debit": 0,
"total_items": 1,
"coa": "1-1101",
"keterangan": "Kas"
}
]
}
Does anyone know how to do it ?. Thanks in advance. I really appreciated the help.
Here it is, You can do like this, I have added the logic
var jsonData = {
"data": [
{
"5-1001": 1,
"sum_kredit": 0,
"sum_debit": 100000,
"total_items": 1
},
{
"1-1101": 1,
"sum_kredit": 100000,
"sum_debit": 0,
"total_items": 1
}
],
"desc_coa": [
{
"coa": "5-1001",
"keterangan": "HPP"
},
{
"coa": "1-1101",
"keterangan": "Kas"
}
]
}
var finalResult = { data: [] }
//Dynamic Code
for(var i=0;i<jsonData.data.length;i++){
finalResult.data.push({ ...jsonData.data[i], ...jsonData.desc_coa[i] })
}
console.log("jsonData..........", finalResult)
OR Static Code
finalResult.data.push({ ...jsonData.data[0], ...jsonData.desc_coa[0] })
finalResult.data.push({ ...jsonData.data[1], ...jsonData.desc_coa[1] })
console.log("jsonData..........", finalResult)
Output
{
"data": [
{
"5-1001": 1,
"sum_kredit": 0,
"sum_debit": 100000,
"total_items": 1,
"coa": "5-1001",
"keterangan": "HPP"
},
{
"1-1101": 1,
"sum_kredit": 100000,
"sum_debit": 0,
"total_items": 1,
"coa": "1-1101",
"keterangan": "Kas"
}
]
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.