Since yesterday, I have successfully tackled my algorithm problem.
Recognizing that the title may be a bit vague, I will do my best to clarify my thoughts.
What I aim to do is transform a table of objects (A) into a new table of objects (B).
The array of objects (A) is structured like this:
{
id : "REC-001",
text: "Facebook",
link: ["REC-002", "REC-003"]
},
{
id : "REC-002",
text: "Instagram",
link: ["REC-003"]
},
{
id : "REC-003",
text: "Snapshat",
link: ["REC-001", "REC-002"]
}
The desired structure for object array (B) is as follows:
{
id : "REC-001",
text: "Facebook",
link: [
{
key: "REC-002",
text: "Instagram"
},
{
key: "REC-003",
text: "Snapshat"
}
]
},
{
id : "REC-002",
text: "Instagram",
link: [
{
key: "REC-003",
text: "Snapshat"
}
]
},
{
id : "REC-003",
text: "Snapshat",
link: [
{
key: "REC-001",
text: "Facebook"
},
{
key: "REC-002",
text: "Instagram"
}
]
}
Currently, the only code snippet that partially aligns with my goal looks like this (everything else has been discarded):
for (let i = 0; i < objectA.length; i++) {
for (let j = 0; j < objectA[i].link.length; j++) {
console.log(i, j, objectA[i].link[j])
};
};
The output of the console.log is:
0 0 'REC-002'
0 1 'REC-003'
1 0 'REC-003'
2 0 'REC-001'
2 1 'REC-002'
Unfortunately, I am struggling to generate an array of "link" objects associated with the main "id". Any assistance would be greatly appreciated!
Thank you,
Sam.