I have a sophisticated object that contains an array of objects which must be added (grouped by field and then added).
Below are the formats for both:
// This is the original object where I want to add the "arr" below based on grouping
let filterObj = {
"feature": "test",
"filter": {
"and": [
{ "field": "field1","value": "1"}
]
}
};
// This array should be added to the above object after grouping by field
let obj = [
{"field": "field1","value": "2"},
{"field": "fiedl2","value": "3"},
{"field" : "field2","value": "4"},
{"field" : "field3","value" : "5"}
]
The desired output format is as follows:
var result = {
"feature": "test",
"filter": {
"and": [
{
"or" : [
{"field": "field1","value": "1"},
{"field": "field1", "value": "2"}
]
},
{
"or" : [
{"field": "field2","value": "3"},
{ "field": "field2","value": "4"},
]
},
{ "field": "field3", "value": "5"}
]
}
}
// The method I attempted
filterObj.filter.and.or(...obj) ;// Does not work
I need to group the elements based on their field values and then insert them into the "or" array of objects if the field values match. Otherwise, they should be directly added to the "and" array of objects.
Any assistance would be greatly appreciated.