title: Grouping and adding array of objects to a complex object: Javascript
body: <p>I have a complex object where some array of objects that needs to be added ( grouping based on field and adding)</p>
  
  <p>The format of both is shown below</p>
  
  <pre><code>//This is the source object where I need to add the below "arr" based on grouping
  
  let filterObj = {
        "feature": "test",
        "filter": {
                   "and": [
                            { "field": "field1","value": "1"}
                          ]
                  }
  };
  
  //This array needs to be added to above object by grouping based on field
  let obj = [
             {"field": "field1","value": "2"},
             {"field": "fiedl2","value": "3"},
             {"field" : "field2","value": "4"},
             {"field" : "field3","value" : "5"}
            ]
  
  </code></pre>
  
  <p>I want the output to be of following format:</p>
  
  <pre><code>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 that I have tried
  
  filterObj.filter.and.or(...obj) ;// Does not work 
  
  
  </code></pre>
  
  <p>I need to group them based on field values and then add them to the "or" array of objects(if field value is same). If not directly add it to the "and" array of objects.</p>
  
  <p>Help would be appreciated.</p>