Я не думаю, что ожидаемый результат, который вы написали, является правильным.По моему мнению, ожидаемый результат должен быть:
{
"Level": [
{
"levelId": 1,
"level": "1",
"points": 15
},
{
"levelId": 2,
"level": "2",
"points": 15
},
{
"levelId": 3,
"level": "3",
"points": 15
}
],
"Medal": [
{
"medalId": 1,
"medalName": "Bronze",
"points": 2020
},
{
"medalId": 2,
"medalName": "Silver",
"points": 10
},
{
"medalId": 3,
"medalName": "Gold",
"points": 10
}
]
}
И код для получения такого вывода:
var abc = {
"levelId-0": 1,
"level-0": "1",
"levelpoints-0": 15,
"levelId-1": 2,
"level-1": "2",
"levelpoints-1": 15,
"levelId-2": 3,
"level-2": "3",
"levelpoints-2": 15,
"levelId-3": 4,
"level-3": "4",
"levelpoints-3": 15,
"medalId-0": 1,
"medalName-0": "Bronze",
"medalsPoint-0": 2020,
"medalId-1": 2,
"medalName-1": "Silver",
"medalsPoint-1": 10,
"medalId-2": 3,
"medalName-2": "Gold",
"medalsPoint-2": 10,
"medalId-3": 1003,
"medalName-3": "Platinum",
"medalsPoint-3": 85
};
var final = {
"Level": [],
"Medal": []
};
let abcKeys = Object.keys(abc);
var lastRowIndex = parseInt(abcKeys[abcKeys.length - 1].split('-')[1]); //3
for (let i = 0; i < lastRowIndex; i++) {
let levelObj = {}, medalObj = {};
levelObj['levelId'] = abc['levelId-' + i];
levelObj['level'] = abc['level-' + i];
levelObj['points'] = abc['levelpoints-' + i];
medalObj['medalId'] = abc['medalId-' + i];
medalObj['medalName'] = abc['medalName-' + i];
medalObj['points'] = abc['medalsPoint-' + i];
final.Level.push(levelObj);
final.Medal.push(medalObj);
}
console.log('--final--', final);