I have a json like this
{
"refURL": "/unifiedconfig/config/agentteam/5022",
"changeStamp": 12,
"agentCount": 7,
"description": "Cumulus Outbound Team",
"name": "CumulusOutbound",
"peripheral": {
"id": 5000,
"name": "CUCM_PG_1"
},
"peripheralId": 5000,
"supervisorCount": 1,
"agents": [{
"agent": [{
"refURL": "/unifiedconfig/config/agent/5197",
"agentId": "1085",
"firstName": "Owen",
"lastName": "Harvey",
"userName": "oharvey"
}, {
"refURL": "/unifiedconfig/config/agent/5201",
"agentId": "1320",
"firstName": "Bruce",
"lastName": "Wayne",
"userName": "bwayne"
}, {
"refURL": "/unifiedconfig/config/agent/5202",
"agentId": "1321",
"firstName": "Peter",
"lastName": "Parker",
"userName": "pparker"
}, {
"refURL": "/unifiedconfig/config/agent/5203",
"agentId": "1322",
"firstName": "Tony",
"lastName": "Stark",
"userName": "tstark"
}, {
"refURL": "/unifiedconfig/config/agent/5204",
"agentId": "1323",
"firstName": "Steve",
"lastName": "Rogers",
"userName": "srogers"
}, {
"refURL": "/unifiedconfig/config/agent/5205",
"agentId": "1324",
"firstName": "Bruce",
"lastName": "Banner",
"userName": "bbanner"
}, {
"refURL": "/unifiedconfig/config/agent/5231",
"agentId": "1086",
"firstName": "Annika",
"lastName": "Hamilton",
"userName": "annika"
}]
}],
"supervisors": [{
"supervisor": [{
"refURL": "/unifiedconfig/config/agent/5174",
"agentId": "1082",
"firstName": "Rick",
"lastName": "Barrows",
"userName": "rbarrows#dcloud.cisco.com"
}]
}]
}
To the above json, i need to add another agent inside of agents[] of agents[], below is the element that i need to add
{"refURL":"/unifiedconfig/config/agent/2255","agentId":"1478","firstName":"kkk","lastName":"ddd","userName":"dddd"}
supposong you're dealing with a json object :
in that case , all you have is just to acess the correct json path then push the new json agent into the array of agents .
here the it should be like yourJsobObject.agents[0].agent
to insert New json just use push : yourJsobObject.agents[0].agent.push(newJsonAgent);
you can check below snippet as an example :
let json = {
"refURL": "/unifiedconfig/config/agentteam/5022",
"changeStamp": 12,
"agentCount": 7,
"description": "Cumulus Outbound Team",
"name": "CumulusOutbound",
"peripheral": {
"id": 5000,
"name": "CUCM_PG_1"
},
"peripheralId": 5000,
"supervisorCount": 1,
"agents": [{
"agent": [{
"refURL": "/unifiedconfig/config/agent/5197",
"agentId": "1085",
"firstName": "Owen",
"lastName": "Harvey",
"userName": "oharvey"
}, {
"refURL": "/unifiedconfig/config/agent/5201",
"agentId": "1320",
"firstName": "Bruce",
"lastName": "Wayne",
"userName": "bwayne"
}, {
"refURL": "/unifiedconfig/config/agent/5202",
"agentId": "1321",
"firstName": "Peter",
"lastName": "Parker",
"userName": "pparker"
}, {
"refURL": "/unifiedconfig/config/agent/5203",
"agentId": "1322",
"firstName": "Tony",
"lastName": "Stark",
"userName": "tstark"
}, {
"refURL": "/unifiedconfig/config/agent/5204",
"agentId": "1323",
"firstName": "Steve",
"lastName": "Rogers",
"userName": "srogers"
}, {
"refURL": "/unifiedconfig/config/agent/5205",
"agentId": "1324",
"firstName": "Bruce",
"lastName": "Banner",
"userName": "bbanner"
}, {
"refURL": "/unifiedconfig/config/agent/5231",
"agentId": "1086",
"firstName": "Annika",
"lastName": "Hamilton",
"userName": "annika"
}]
}],
"supervisors": [{
"supervisor": [{
"refURL": "/unifiedconfig/config/agent/5174",
"agentId": "1082",
"firstName": "Rick",
"lastName": "Barrows",
"userName": "rbarrows#dcloud.cisco.com"
}]
}]
}
let newAgent = {"refURL":"/unifiedconfig/config/agent/2255","agentId":"1478","firstName":"kkk","lastName":"ddd","userName":"dddd"}
json.agents[0].agent.push(newAgent);
console.log(json.agents[0].agent)
Related
This question already has answers here:
Copy array items into another array
(19 answers)
Closed 10 days ago.
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"
}
]
}
]
}
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
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 ];
This is my json data:
{
"rows": [
{
"id": 3,
"code": "airtel121",
"position": "manager",
"salary": "25000",
"login": {
"id": 4,
"username": "sameer",
"firstName": "Mohamed",
"lastName": "Sameer",
"code": "airtel121",
}
}
]
}
My Expected output:
{
"rows": [
{
"id": 4,
"username": "sameer",
"firstName": "Mohamed",
"lastName": "Sameer",
"code": "airtel121",
"staffs": [
{
"id": 3,
"code": "airtel121",
"position": "manager",
"salary": "25000",
}
]
},
]
}
I want to exchange the first object into one array as staff, i dont know what kind of library or method is to make this stuff,
Like this? You can use ES 2018 Object spread to pull the properties you want into an object literal.
let a = {
"rows": [
{
"id": 3,
"code": "airtel121",
"position": "manager",
"salary": "25000",
"login": {
"id": 4,
"username": "sameer",
"firstName": "Mohamed",
"lastName": "Sameer",
"code": "airtel121",
}
}
]
}
let b = { "rows": [{ ...a.rows[0].login, "staffs": [ { ...a.rows[0] } ] } ] }
delete(b.rows[0].staffs[0].login)
console.log(b)
There are multiple ways to solve this problem. With simple javascript you can do it like this.
var data = {
"rows": [
{
"id": 3,
"code": "airtel121",
"position": "manager",
"salary": "25000",
"login": {
"id": 4,
"username": "sameer",
"firstName": "Mohamed",
"lastName": "Sameer",
"code": "airtel121",
}
}
]
}
var exchangedData = data["rows"].map((row) => {
row["id"] = row["login"]["id"]
row["username"] = row["login"]["username"]
row["firstName"] = row["login"]["firstName"]
row["lastName"] = row["login"]["lastName"]
row["code"] = row["login"]["code"]
row["staffs"] = [{ id: row["id"], code: row["code"], position: row["position"], salary: row["salary"]}]
delete row["login"]
delete row["position"]
delete row["salary"]
return row;
})
console.log(exchangedData)
var rows = {
"rows": [
{
"id": 3,
"code": "airtel121",
"position": "manager",
"salary": "25000",
"login": {
"id": 4,
"username": "sameer",
"firstName": "Mohamed",
"lastName": "Sameer",
"code": "airtel121",
}
}
]
};
var data = {rows:[]}
rows.rows.forEach(function(value, index){
data.rows.push(
_.assign(
{},
{
"id": value.login.id,
"username": value.login.username,
"firstName": value.login.firstName,
"lastName": value.login.lastName,
"code": value.login.code,
},{"staffs":[{"id":value.id, "code":value.code, "position": value.position, "salary": value.salary}]})
)});
console.log(data);
I think this is a much simpler solution that handles any number of objects in your rows array.
result = {
"rows": [
{
"id": 3,
"code": "airtel121",
"position": "manager",
"salary": "25000",
"login": {
"id": 4,
"username": "sameer",
"firstName": "Mohamed",
"lastName": "Sameer",
"code": "airtel121",
}
}
]
}
const data = result.rows.map(d => {
const {id, code, position, salary} = d
return Object.assign(d.login, {staffs: {...{id, code, position, salary}}})
})
console.log(data)
I want to copy employee to a new employee and not point to an old employee.
I try to use:
newEmployees.assign([{}].employees); <br>
newEmployees = [{...employees}]; <br>
They are pointing to an old employee.
const employees = [
{
"id": "1001",
"firstname": "Luke",
"lastname": "Skywalker",
"company": "Walt Disney",
"salary": "40000"
},
{
"id": "1002",
"firstname": "Tony",
"lastname": "Stark",
"company": "Marvel",
"salary": "1000000"
},
{
"id": "1003",
"firstname": "Somchai",
"lastname": "Jaidee",
"company": "Love2work",
"salary": "20000"
},
{
"id": "1004",
"firstname": "Monkey D",
"lastname": "Luffee",
"company": "One Piece",
"salary": "9000000"
}
];
let newEmployees = [{}];
newEmployees[0] = employees[3]; // assign
employees[3]['firstname'] = 'Arhus'; // change old employees
console.log(newEmployees); //firstname: 'Arhus' - shows employees content
Thank you so much.
Just use spread while assigning the value like this
const employees = [
{
"id": "1001",
"firstname": "Luke",
"lastname": "Skywalker",
"company": "Walt Disney",
"salary": "40000"
},
{
"id": "1002",
"firstname": "Tony",
"lastname": "Stark",
"company": "Marvel",
"salary": "1000000"
},
{
"id": "1003",
"firstname": "Somchai",
"lastname": "Jaidee",
"company": "Love2work",
"salary": "20000"
},
{
"id": "1004",
"firstname": "Monkey D",
"lastname": "Luffee",
"company": "One Piece",
"salary": "9000000"
}
];
let newEmployees = [{}];
newEmployees[0] = { ...employees[3] };
employees[3]['firstname'] = 'Arhus'; // change employee
console.log(newEmployees); //firstname is still Monkey D
Just use .map and object spread:
const employees = [
{
"id": "1001",
"firstname": "Luke",
"lastname": "Skywalker",
"company": "Walt Disney",
"salary": "40000"
},
{
"id": "1002",
"firstname": "Tony",
"lastname": "Stark",
"company": "Marvel",
"salary": "1000000"
},
{
"id": "1003",
"firstname": "Somchai",
"lastname": "Jaidee",
"company": "Love2work",
"salary": "20000"
},
{
"id": "1004",
"firstname": "Monkey D",
"lastname": "Luffee",
"company": "One Piece",
"salary": "9000000"
}
];
const newEmployees = employees.map(employee => ({...employee}));
employees[3].firstname = 'Arhus';
console.log(newEmployees[3]);
console.log(employees[3]);
While assigning value you can use Object.assign or Object.clone
Consider the following snippet:
const employees = [
{
"id": "1001",
"firstname": "Luke",
"lastname": "Skywalker",
"company": "Walt Disney",
"salary": "40000"
},
{
"id": "1002",
"firstname": "Tony",
"lastname": "Stark",
"company": "Marvel",
"salary": "1000000"
},
{
"id": "1003",
"firstname": "Somchai",
"lastname": "Jaidee",
"company": "Love2work",
"salary": "20000"
},
{
"id": "1004",
"firstname": "Monkey D",
"lastname": "Luffee",
"company": "One Piece",
"salary": "9000000"
}
];
let newEmployees = [{}];
newEmployees[0] = Object.assign({}, employees[3]);
employees[3]['firstname'] = 'Arhus';
console.log(newEmployees);
I understand you want to copy the object value and don't want to use stringify. Stringifying the object and parsing it back to JSON is one way,
cloning the object is another method and I believe spreading also works.
But if you prefer convenience you may want to take a look at "lodash" library's _.clone and _.clonedeep methods.
Also if you'd like to read more refer :
Lodash .clone and .cloneDeep behaviors
How to copy an object by value, not by reference
I have a selected list object like this {"0":"1","2":"1"},
I want to compare it with another array like the following
{
"0": {
"id": 1,
"salutation": "Dr.",
"firstname": "Kapil",
"lastname": "Dev",
"gender": "Male ",
"email": "kapil.dev#aggenome.com",
"phone": 1232423415,
"usertype": "student",
"institution": "AgriGenome Labs Pvt Ltd",
"department": "Lab",
"country": "India",
"conferenceitem": "2017 NGBT Conference ",
"conferenceitemid": "39",
"amount": 2800,
"actual_amount": "5000.00",
"currency": "INR",
"group": "Lead",
"accompany": "No",
"password": null,
"mailsend": "Yes"
},
"1": {
"id": 2,
"salutation": "Mr.",
"firstname": "Sunil",
"lastname": "Gavaskar",
"gender": "Male ",
"email": "sunil.gavaskar#aggenome.com",
"phone": 1232423415,
"usertype": "commercial",
"institution": "AgriGenome Labs Pvt Ltd",
"department": "Bio Info",
"country": "India",
"conferenceitem": "2017 NGBT Conference ",
"conferenceitemid": "31",
"amount": "3100.00",
"actual_amount": "10000.00",
"currency": "INR",
"group": "Yes",
"accompany": "No",
"password": null,
"mailsend": "Yes"
},
"2": {
"id": 3,
"salutation": "Mr.",
"firstname": "Anil",
"lastname": "Kumble",
"gender": "Male ",
"email": "anil.kumble#aggenome.com",
"phone": 1232423415,
"usertype": "student",
"institution": "AgriGenome Labs Pvt Ltd",
"department": "Support",
"country": "India",
"conferenceitem": "Accompanying Person",
"conferenceitemid": "5",
"amount": 1900,
"actual_amount": "5000.00",
"currency": "INR",
"group": "No",
"accompany": "Yes",
"password": null,
"mailsend": "No"
}
}
on the basis of the keys, means that only 0 & 2 are selected and I need to fetch data from the second object having key 0 & 2, (excluding 1 ), how can I do this? I am new to this area...
var obj = {"0":"1","2":"1"};
var newobj = {
"0": {
"id": 1,
"salutation": "Dr.",
"firstname": "Kapil",
"lastname": "Dev",
"gender": "Male ",
"email": "kapil.dev#aggenome.com",
"phone": 1232423415,
"usertype": "student",
"institution": "AgriGenome Labs Pvt Ltd",
"department": "Lab",
"country": "India",
"conferenceitem": "2017 NGBT Conference ",
"conferenceitemid": "39",
"amount": 2800,
"actual_amount": "5000.00",
"currency": "INR",
"group": "Lead",
"accompany": "No",
"password": null,
"mailsend": "Yes"
},
"1": {
"id": 2,
"salutation": "Mr.",
"firstname": "Sunil",
"lastname": "Gavaskar",
"gender": "Male ",
"email": "sunil.gavaskar#aggenome.com",
"phone": 1232423415,
"usertype": "commercial",
"institution": "AgriGenome Labs Pvt Ltd",
"department": "Bio Info",
"country": "India",
"conferenceitem": "2017 NGBT Conference ",
"conferenceitemid": "31",
"amount": "3100.00",
"actual_amount": "10000.00",
"currency": "INR",
"group": "Yes",
"accompany": "No",
"password": null,
"mailsend": "Yes"
},
"2": {
"id": 3,
"salutation": "Mr.",
"firstname": "Anil",
"lastname": "Kumble",
"gender": "Male ",
"email": "anil.kumble#aggenome.com",
"phone": 1232423415,
"usertype": "student",
"institution": "AgriGenome Labs Pvt Ltd",
"department": "Support",
"country": "India",
"conferenceitem": "Accompanying Person",
"conferenceitemid": "5",
"amount": 1900,
"actual_amount": "5000.00",
"currency": "INR",
"group": "No",
"accompany": "Yes",
"password": null,
"mailsend": "No"
}
}
var newArray = Object.keys(obj).map(item => {
return newobj[item]})
console.log(newArray)
Considering you only want to the object filtered out from your selection, you could use forEach on Object.keys of your selected object
var obj = {"0":"1","2":"1"};
var newobj = {
"0": {
"id": 1,
"salutation": "Dr.",
"firstname": "Kapil",
"lastname": "Dev",
"gender": "Male ",
"email": "kapil.dev#aggenome.com",
"phone": 1232423415,
"usertype": "student",
"institution": "AgriGenome Labs Pvt Ltd",
"department": "Lab",
"country": "India",
"conferenceitem": "2017 NGBT Conference ",
"conferenceitemid": "39",
"amount": 2800,
"actual_amount": "5000.00",
"currency": "INR",
"group": "Lead",
"accompany": "No",
"password": null,
"mailsend": "Yes"
},
"1": {
"id": 2,
"salutation": "Mr.",
"firstname": "Sunil",
"lastname": "Gavaskar",
"gender": "Male ",
"email": "sunil.gavaskar#aggenome.com",
"phone": 1232423415,
"usertype": "commercial",
"institution": "AgriGenome Labs Pvt Ltd",
"department": "Bio Info",
"country": "India",
"conferenceitem": "2017 NGBT Conference ",
"conferenceitemid": "31",
"amount": "3100.00",
"actual_amount": "10000.00",
"currency": "INR",
"group": "Yes",
"accompany": "No",
"password": null,
"mailsend": "Yes"
},
"2": {
"id": 3,
"salutation": "Mr.",
"firstname": "Anil",
"lastname": "Kumble",
"gender": "Male ",
"email": "anil.kumble#aggenome.com",
"phone": 1232423415,
"usertype": "student",
"institution": "AgriGenome Labs Pvt Ltd",
"department": "Support",
"country": "India",
"conferenceitem": "Accompanying Person",
"conferenceitemid": "5",
"amount": 1900,
"actual_amount": "5000.00",
"currency": "INR",
"group": "No",
"accompany": "Yes",
"password": null,
"mailsend": "No"
}
}
const result = {}
Object.keys(obj).forEach(key => {
result[key] = newobj[key]
})
console.log(result)
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
</body>
<script type="text/javascript">
var flag = {"0":"1","2":"1"};
var data = {"0":{"id":1,"salutation":"Dr.","firstname":"Kapil","lastname":"Dev","gender":"Male ","email":"kapil.dev#aggenome.com","phone":1232423415,"usertype":"student","institution":"AgriGenome Labs Pvt Ltd","department":"Lab","country":"India","conferenceitem":"2017 NGBT Conference ","conferenceitemid":"39","amount":2800,"actual_amount":"5000.00","currency":"INR","group":"Lead","accompany":"No","password":null,"mailsend":"Yes"},"1":{"id":2,"salutation":"Mr.","firstname":"Sunil","lastname":"Gavaskar","gender":"Male ","email":"sunil.gavaskar#aggenome.com","phone":1232423415,"usertype":"commercial","institution":"AgriGenome Labs Pvt Ltd","department":"Bio Info","country":"India","conferenceitem":"2017 NGBT Conference ","conferenceitemid":"31","amount":"3100.00","actual_amount":"10000.00","currency":"INR","group":"Yes","accompany":"No","password":null,"mailsend":"Yes"},"2":{"id":3,"salutation":"Mr.","firstname":"Anil","lastname":"Kumble","gender":"Male ","email":"anil.kumble#aggenome.com","phone":1232423415,"usertype":"student","institution":"AgriGenome Labs Pvt Ltd","department":"Support","country":"India","conferenceitem":"Accompanying Person","conferenceitemid":"5","amount":1900,"actual_amount":"5000.00","currency":"INR","group":"No","accompany":"Yes","password":null,"mailsend":"No"}};
//loop the flag
$.each( flag, function(i,c){
//loop the data
$.each(data,function(di,dc){
if(i == di)
{
//data you want
console.log(dc)
}
});
});
</script>
</html>
I have two arrays:
First array:
generalInfo = [{"fName": "Suku", "lName": "Brown", "sex": "Male", "Id": "RY12S"}, {"fName": "Mary", "lName": "Sue", "sex": "Female", "Id": "UT72W"}]
Second array:
paymentInfo = [{"Id": "RY12S", "amount": "1000.00", "purpose": "rent", "date": "2017-17-07"}, {"Id": "UT72W", "amount": "5000.00", "purpose": "renovation", "date": "2017-15-07"}]
Now, I want to be able to compare the Id field for the both arrays and return their unique information.
At least the third should look like this:
specificInfo = [{"Id": "RY12S","fName": "Suku", "lName": "Brown", "amount": "1000.00", "purpose": "rent"}, {"Id": "UT72W","fName": "Mary", "lName": "Sue", "amount": "5000.00", "purpose": "renovation"}]
Is there any easy way to achieve this?
An ES6 solution with map and find:
var generalInfo = [{"fName": "Suku", "lName": "Brown", "sex": "Male", "Id": "RY12S"}, {"fName": "Mary", "lName": "Sue", "sex": "Female", "Id": "UT72W"}];
var paymentInfo = [{"Id": "RY12S", "amount": "1000.00", "purpose": "rent", "date": "2017-17-07"}, {"Id": "UT72W", "amount": "5000.00", "purpose": "renovation", "date": "2017-15-07"}];
var uniqueInfo = generalInfo.map((generalItem) => {
var paymentItem = paymentInfo.find((item) => item.Id === generalItem.Id);
return {
Id: generalItem.Id,
fName: generalItem.fName,
lName: generalItem.lName,
amount: paymentItem.amount,
purpose: paymentItem.purpose
};
});
console.log(uniqueInfo);
Try to understand the logic behind this and, if you need to implement in ES5, try to do by yourself before posting here.