Given a JSON file, I am looking to group objects based on common x and y values. Essentially, I want to group together objects that share the same x
and y
properties.
Here is an example of the JSON data:
let data = [{
"x": "0",
"y": "0",
"k": "0"
},
{
"x": "0",
"y": "0",
"k": "1"
},
{
"x": "1",
"y": "2",
"k": "0"
},
{
"x": "1",
"y": "2",
"k": "5"
},
{
"x": "2",
"y": "2",
"k": "10"
},
{
"x": "1",
"y": "2",
"k": "12"
}
]
The desired result would be as follows:
result = [
[{
"x": "0",
"y": "0",
"k": "0"
},
{
"x": "0",
"y": "0",
"k": "1"
}],
[
{
"x": "1",
"y": "2",
"k": "0"
},
{
"x": "1",
"y": "2",
"k": "5"
},
{
"x": "1",
"y": "2",
"k": "12"
}
],
[{
"x": "2",
"y": "2",
"k": "10"
}]
]
I only need to separate the objects with common x
and y
,
how can I go about solving this issue?