Good day, I hope someone can help.
I have an array of objects that looks like this that I'm looking to reduce the objects a little:
[
{
"entityName": "0015J00000BUWbHQAX",
"countryCode": "BE",
"totalReclaimPotential": 2506.0360930000006
},
{
"entityName": "0015J00000BUWbHQAX",
"countryCode": "DK",
"totalReclaimPotential": 1932.5224279999995
},
{
"entityName": "0015J00000BUWbHQAX",
"countryCode": "PT",
"totalReclaimPotential": 586.8
},
{
"entityName": "0015J00000BUWbHQAX",
"countryCode": "NL",
"totalReclaimPotential": 569.1673320000001
},
{
"entityName": "0015J00000BUWbHQAX",
"countryCode": "CH",
"totalReclaimPotential": 544.3347689999999
},
{
"entityName": "0015J00000BUWbHQAX",
"countryCode": "ES",
"totalReclaimPotential": 453.127841
},
{
"entityName": "0015J00000BUWbHQAX",
"countryCode": "KR",
"totalReclaimPotential": 448.640522
},
{
"entityName": "0015J00000BUWbHQAX",
"countryCode": "TW",
"totalReclaimPotential": 204.25425299999998
},
]
What I'm trying to do is combine all the objects after index 3 to the same object. It should look something like this:
[
{
"entityName": "0015J00000BUWbHQAX",
"countryCode": "BE",
"totalReclaimPotential": 2506.0360930000006
},
{
"entityName": "0015J00000BUWbHQAX",
"countryCode": "DK",
"totalReclaimPotential": 1932.5224279999995
},
{
"entityName": "0015J00000BUWbHQAX",
"countryCode": "PT",
"totalReclaimPotential": 586.8
},
{
"entityName": "0015J00000BUWbHQAX",
"countryCode": "NL",
"totalReclaimPotential": 569.1673320000001
},
{
"entityName": "Other Entities",
"countryCode": "",
"totalReclaimPotential": <Total of all merged objects>
},
]
How can I achieve this using Angular/Typescript?
CodePudding user response:
const data = [{
"entityName": "0015J00000BUWbHQAX",
"countryCode": "BE",
"totalReclaimPotential": 2506.0360930000006
},
{
"entityName": "0015J00000BUWbHQAX",
"countryCode": "DK",
"totalReclaimPotential": 1932.5224279999995
},
{
"entityName": "0015J00000BUWbHQAX",
"countryCode": "PT",
"totalReclaimPotential": 586.8
},
{
"entityName": "0015J00000BUWbHQAX",
"countryCode": "NL",
"totalReclaimPotential": 569.1673320000001
},
{
"entityName": "0015J00000BUWbHQAX",
"countryCode": "CH",
"totalReclaimPotential": 544.3347689999999
},
{
"entityName": "0015J00000BUWbHQAX",
"countryCode": "ES",
"totalReclaimPotential": 453.127841
},
{
"entityName": "0015J00000BUWbHQAX",
"countryCode": "KR",
"totalReclaimPotential": 448.640522
},
{
"entityName": "0015J00000BUWbHQAX",
"countryCode": "TW",
"totalReclaimPotential": 204.25425299999998
},
]
const result = data.slice(0, 4).concat(data.slice(4).reduce((acc, cur) => {
acc.totalReclaimPotential = cur.totalReclaimPotential;
return acc;
}, {
entityName: "Other Entities",
countryCode: "",
totalReclaimPotential: 0
}));
console.log('result:', result);
CodePudding user response:
let shortenArrToLength = 4;
let total = 0;
values.map((obj, index) => {
if(index < shortenArrToLength) {
return;
}
total = obj['totalReclaimPotential'];
})
values.length = shortenArrToLength;
values.push({
"entityName": "Other Entities",
"countryCode": "",
"totalReclaimPotential": total
})
console.log(values);