I'm facing a challenge with a JavaScript array where I need to extract teams (Team A, Team B) and organize them into one array while preserving their order. See below for the current output of the array, as well as the JS code provided and the expected outcome.
Current Array:
[
{
"Team A": 2,
"Team B": 3
}
]
JS Code:
var retValue = [];
var count = this.jsonArray.map((cv) => {
var teams = cv["teams"].reduce((acc, team) => {
acc[team["value"]] = team["text"];
return acc;
}, {});
return cv["duty"].reduce((acc, duty) => {
acc[teams[duty["parentId"]]] = acc[
teams[duty["parentId"]]
]
? acc[teams[duty["parentId"]] + 1
: 1;
return acc;
}, {});
});
retValue = Object.keys(count).map((cv) => count[cv]);
console.debug(retValue); //working
var getKeys = [];
getKeys = Object.keys(retValue); //not working
var getValues = [];
....
Expected Result:
[Team A, Team B] , [2 , 3]
Original JSON Data:
{
"jsonArray": [{
"teams": [
{
"text": "Team A",
"id": 1,
"value": 500,
"parentId": 333
},
{
"text": "Team B",
"id": 2,
"value": 600,
"parentId": 444
}
],
"duty": [
{
"text": "Meetings",
"id": 11,
"value": 100,
"parentId": 500
},
{
"text": "Lunch",
"id": 12,
"value": 101,
"parentId": 500
},
{
"text": "Time Cards",
"id": 13,
"value": 102,
"parentId": 600
},
{
"text": "Parking",
"id": 14,
"value": 103,
"parentId": 600
},
{
"text": "Breakfast",
"id": 15,
"value": 104,
"parentId": 600
}
]
}]
};