将对象数组添加到现有对象

时间:2019-10-24 06:04:01

标签: javascript arrays object

我有一个如下所示的params对象

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

我想要的对象应该在后面

{
    "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"
            }]
        }]
    }
}

我创建了以下

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

在其中添加以下具有逻辑的部分

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

但是无法添加以上内容以获得完整的对象

2 个答案:

答案 0 :(得分:1)

您需要在filter_list键上创建query_options

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)

答案 1 :(得分:1)

您可以将数据添加为:     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);