Essentially, I have an array of objects structured as follows:
connectionInfo: any = [
{
"deviceName": "transponder1",
"ports": 4,
"rowNo": 1,
"columnNo": 1,
"posX": 2060.5,
"posY": 200,
"portInfo": {
"portNumber": "1",
"x": "2260.5",
"y": "261.6666666666667"
}
},
{
"deviceName": "transponder1",
"ports": 4,
"rowNo": 1,
"columnNo": 1,
"posX": 2060.5,
"posY": 200,
"portInfo": {
"portNumber": "2",
"x": "2260.5",
"y": "395"
}
},
{
"deviceName": "transponder1",
"ports": 4,
"rowNo": 1,
"columnNo": 1,
"posX": 2060.5,
"posY": 200,
"portInfo": {
"portNumber": "3",
"x": "2260.5",
"y": "528.3333333333334"
}
}
];
The desired output should be in the following format:
{
"deviceName": "transponder1",
"ports": 4,
"rowNo": 1,
"columnNo": 1,
"posX": 2060.5,
"posY": 200,
"portInfo": {
"portNumber": "1",
"x": "2260.5",
"y": "261.6666666666667"
},
"allPortsInfo": [
{
"portNumber": "1",
"x": "2260.5",
"y": "261.6666666666667"
},
{
"portNumber": "2",
"x": "2260.5",
"y": "395"
},
{
"portNumber": "3",
"x": "2260.5",
"y": "528.3333333333334"
},
{
"portNumber": "4",
"x": "2260.5",
"y": "528.3333333333334"
}
]
}
My attempt involved using the following code snippet:
const cInfo = this.connectionInfo.reduce((acc, value) => acc.concat(value.portInfo),[]);However, with the above approach, the output ends up being:
[
{
"portNumber": "1",
"x": "2260.5",
"y": "261.6666666666667"
},
{
"portNumber": "2",
"x": "2260.5",
"y": "395"
},
{
"portNumber": "3",
"x": "2260.5",
"y": "528.3333333333334"
},
{
"portNumber": "4",
"x": "2260.5",
"y": "528.3333333333334"
}
]
I utilized reduce to gather all portInfo into an array, however, achieving the expected output within the reduce loop remains uncertain.
You can view my code on StackBlitz