Gaurav
Gaurav

Reputation: 37

adding array of objects to existing objects

I have an existing params object as following

{
    "application_context": {
        "application_id": "a0",
        "context_id": "c0"
    },
    "device_data": {
        "device_id": "",
        "hostname": ""
    },
    "query_options": {
        "page_token": "",
        "page_size": 30
    }
}

My desired object should be following

{
    "application_context": {
        "application_id": "a0",
        "context_id": "c0"
    },
    "device_data": {
        "device_id": "",
        "hostname": ""
    },
    "query_options": {
        "page_token": "",
        "page_size": 30,
        "filter_list": [{
            "operator": "OPERATOR_AND",
            "field_list": [{
                "field": "State",
                "value": "INACTIVE_STATE"
            }]
        }]
    }
}

I have created the following

param['filter_list'] = [{
            'operator': 'OPERATOR_AND',
             'field_list': filter
           }];

where it's adding the following part with some logic

"field": "State",
"value": "INACTIVE_STATE" 

But not able to add the above to get my complete object

Upvotes: 0

Views: 51

Answers (2)

Bilal Siddiqui
Bilal Siddiqui

Reputation: 3629

You can add your data as: obj["query_options"]["filter_list"] = param["filter_list"];

var obj =  {
        "application_context": {
            "application_id": "a0",
            "context_id": "c0"
        },
        "device_data": {
            "device_id": "",
            "hostname": ""
        },
        "query_options": {
            "page_token": "",
            "page_size": 30
        }
    };
    
   var filter = [{
                    "field": "State",
                    "value": "INACTIVE_STATE"
                }];

   /*param['filter_list'] = */   
   var filter_list = [{
                "operator": "OPERATOR_AND",
                "field_list": filter
            }]
            
   obj["query_options"]["filter_list"] =  filter_list     ;
   
   console.log(obj);

Upvotes: 1

brk
brk

Reputation: 50291

You need to create filter_list on query_options key

let data = {
  "application_context": {
    "application_id": "a0",
    "context_id": "c0"
  },
  "device_data": {
    "device_id": "",
    "hostname": ""
  },
  "query_options": {
    "page_token": "",
    "page_size": 30
  }
}

data.query_options.filter_list = [{
  "operator": "OPERATOR_AND",
  "field_list": [{
    "field": "State",
    "value": "INACTIVE_STATE"
  }]
}];


console.log(data)

Upvotes: 1

Related Questions