skip to Main Content

Here i have Array of objects, in this agents array has 0 elements. i have add an object to this. "agents": [{}] in this i want to add below element:

{
    "refURL": "/unifiedconfig/config/agentteam/5020",
    "changeStamp": 18,
    "agentCount": 0,
    "description": "Cumulus All Team",
    "name": "CumulusAll",
    "peripheral": {
        "id": 5000,
        "name": "CUCM_PG_1"
    },
    "peripheralId": 5000,
    "supervisorCount": 0,
    "agents": [
        {}
    ]
}

I want to add below element to the above in agents array "agents" [{}]

{
    "agent": [
        {
            "refURL": "/unifiedconfig/config/agent/5101",
            "agentId": "1300",
            "firstName": "Sammy",
            "lastName": "Jackson",
            "userName": "cgjackson"
        },
        {
            "refURL": "/unifiedconfig/config/agent/5106",
            "agentId": "1305",
            "firstName": "Angel",
            "lastName": "Jolie",
            "userName": "cgjolie"
        },
        {
            "refURL": "/unifiedconfig/config/agent/5109",
            "agentId": "1308",
            "firstName": "Steve",
            "lastName": "O",
            "userName": "cgsteveo"
        }
    ]
}

This is the final output i want to be achieved

{
    "refURL": "/unifiedconfig/config/agentteam/5016",
    "changeStamp": 201,
    "agentCount": 3,
    "description": "Cumulus Finance Team",
    "name": "CumulusFinance",
    "peripheral": {
        "id": 5000,
        "name": "CUCM_PG_1"
    },
    "peripheralId": 5000,
    "supervisorCount": 1,
    "agents": [
        {
            "agent": [
                {
                    "refURL": "/unifiedconfig/config/agent/5101",
                    "agentId": "1300",
                    "firstName": "Sammy",
                    "lastName": "Jackson",
                    "userName": "cgjackson"
                },
                {
                    "refURL": "/unifiedconfig/config/agent/5106",
                    "agentId": "1305",
                    "firstName": "Angel",
                    "lastName": "Jolie",
                    "userName": "cgjolie"
                },
                {
                    "refURL": "/unifiedconfig/config/agent/5109",
                    "agentId": "1308",
                    "firstName": "Steve",
                    "lastName": "O",
                    "userName": "cgsteveo"
                }
            ]
        }
    ],
    "supervisors": [
        {
            "supervisor": [
                {
                    "refURL": "/unifiedconfig/config/agent/5174",
                    "agentId": "1082",
                    "firstName": "Rick",
                    "lastName": "Barrows",
                    "userName": "[email protected]"
                }
            ]
        }
    ]
}

2

Answers


  1. const obj = {"refURL":"/unifiedconfig/config/agentteam/5020","changeStamp":18,"agentCount":0,"description":"Cumulus All Team","name":"CumulusAll","peripheral":{"id":5000,"name":"CUCM_PG_1"},"peripheralId":5000,"supervisorCount":0,"agents":[{}]}
    
    const obj1= {
    "agent":[{"refURL":"/unifiedconfig/config/agent/5101","agentId":"1300","firstName":"Sammy","lastName":"Jackson","userName":"cgjackson"},{"refURL":"/unifiedconfig/config/agent/5106","agentId":"1305","firstName":"Angel","lastName":"Jolie","userName":"cgjolie"},{"refURL":"/unifiedconfig/config/agent/5109","agentId":"1308","firstName":"Steve","lastName":"O","userName":"cgsteveo"}]
    }
    
    obj.agents.splice(0,1,obj1);
    console.log(JSON.stringify(obj))
    

    I replaced answer again, please check.

    > Blockquote
    
    Login or Signup to reply.
  2. You can either do it via expression or side-effect. I’m going to name your object oldObject and your agents object agentsObject.

    Expression:

    const newObject = {
        ...oldObject,
        agents: [ agentsObject ],
    };
    

    Side-effect:

    oldObject.agents = [ agentsObject ];
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search