-3

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]"
                }
            ]
        }
    ]
}
7
  • It isn't clear why you don't know how to use array.push developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Feb 10, 2023 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 Commented Feb 10, 2023 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. Commented Feb 10, 2023 at 11:01
  • BTW are you sure about the final output? It's weird to have data.agents.agent being an array. Commented Feb 10, 2023 at 11:14
  • input.agents[0] = input2.agent? Commented Feb 10, 2023 at 11:14

2 Answers 2

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 ];
Sign up to request clarification or add additional context in comments.

5 Comments

i tried side-effect method, it is not giving correct result, it is not adding agent label inside agents.
Did you name the variables correctly?
i posted how my final out put should look like
@GaneshPutta your question got deleted, but I updated my answer anyway.
Thanks for the response. But here one thing is missng. Actually it should be like this "agents":[{"agent":[{..., but from your answer i got "agents":{"agent":[{... Everything is same, but missing "[" before agent, can u please take a look. Thanks in advance
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

3 Comments

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, i tried but it is not giving the correct output. I added expected result in my question. can u please take a look
Please check my code again. At first, I misunderstood your code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.