As someone new to ES6 syntax, I could use some assistance with this task.
I have a JSON array structured like so:
var data = [{
"recordid": 1,
"recordidclass": "Parent Class",
"relatedrecid": 2,
"relatedrecclass": "Child Class2"
},
{
"recordid": 1,
"recordidclass": "Parent Class",
"relatedrecid": 3,
"relatedrecclass": "Child Class3"
},
{
"recordid": 9,
"recordidclass": "Parent Class",
"relatedrecid": 14,
"relatedrecclass": "Child Class4"
},
{
"recordid": 2,
"recordidclass": "Parent Class",
"relatedrecid": 5,
"relatedrecclass": "Child Class5"
},
{
"recordid": 5,
"recordidclass": "Parent Class",
"relatedrecid": 6,
"relatedrecclass": "Child Class6"
},
{
"recordid": 3,
"recordidclass": "Parent Class",
"relatedrecid": 7,
"relatedrecclass": "Child Class7"
},
{
"recordid": 4,
"recordidclass": "Parent Class",
"relatedrecid": 8,
"relatedrecclass": "Child Class8"
},{...}]
All parent and child objects are stored within the same array.
The goal is to filter the data based on the recordid
, retrieving all objects that match the recordid
as well as their corresponding relatedrecid
objects.
In simple terms, given the sample data, calling getRequiredData(1)
(representing the recordid
) should return all data entries that have matching recordid
values along with their associated relatedrecid
values.
The desired output should look like this:
var result = [{
"recordid": 1,
"recordidclass": "Parent Class",
"relatedrecid": 2,
"relatedrecclass": "Child Class2"
},
{
"recordid": 1,
"recordidclass": "Parent Class",
"relatedrecid": 3,
"relatedrecclass": "Child Class3"
},
{
"recordid": 2,
"recordidclass": "Parent Class",
"relatedrecid": 5,
"relatedrecclass": "Child Class5"
},
{
"recordid": 3,
"recordidclass": "Parent Class",
"relatedrecid": 7,
"relatedrecclass": "Child Class7"
}]