-4

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": "rbarrows@dcloud.cisco.com"
                }
            ]
        }
    ]
}
Robo Robok
  • 21,132
  • 17
  • 68
  • 126
Ganesh Putta
  • 2,622
  • 2
  • 19
  • 26
  • It isn't clear why you don't know how to use array.push https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push – Andrew Parks Feb 10 '23 at 10:58
  • is the problem how to add an element to an array? did you just try `.push()`? anyway I'm not sure to understand but you have `.agent` as an array (contrary to what the singular name suggests) maybe you need to merge that array inside the `.agents` array? in that case also `obj.agents = [...obj.agents, ...obj2.agent]` would work. Anyway you should really make the question more clear – Diego D Feb 10 '23 at 10:58
  • Your question is unclear. If you do not mean to push, but to replace agents[0] (plural) by the agent (singular) array, then Object.assign(agents[0], agent) might be what you are looking for. Please be more explicit in what you want to achieve. – Markus Buschhoff Feb 10 '23 at 11:01
  • BTW are you sure about the final output? It's weird to have `data.agents.agent` being an array. – Robo Robok Feb 10 '23 at 11:14
  • `input.agents[0] = input2.agent`? – adiga Feb 10 '23 at 11:14
  • @RoboRobok, yes i copied final out from the working sample. i has to be like that only – Ganesh Putta Feb 10 '23 at 11:16
  • What was wrong about my previous statement to use Object.assign(agents[0], agent)? – Markus Buschhoff Feb 10 '23 at 12:10

2 Answers2

1

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 ];
Robo Robok
  • 21,132
  • 17
  • 68
  • 126
0
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
tzztson
  • 328
  • 3
  • 22
  • This neither merges the 2 arrays nor replaces one with the other. This does a bit of both. If there are 4 items in `obj.agents` and 3 items in `obj1.agent`, it will replace the first 3 items in `obj.agents` with 3 items from `obj1.agent` and will keep the final item in `agents` as is. – adiga Feb 10 '23 at 11:02
  • @adiga, i tried but it is not giving the correct output. I added expected result in my question. can u please take a look – Ganesh Putta Feb 10 '23 at 11:11
  • Please check my code again. At first, I misunderstood your code. – tzztson Feb 10 '23 at 11:32