Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have the following which is almost where I need to be
var baseData= [{
"id": 1,
"username": "JJ",
"lastLoggedIn": null,
"archive": false,
"forename": "J",
"surname": "J",
"band": "Band 7",
"role": "Developer",
"competency": "Kicks",
"employeeNumber": null,
"name": "Sedation"
}, {
"id": 1,
"username": "JJ",
"lastLoggedIn": null,
"archive": false,
"forename": "J",
"surname": "J",
"band": "Band 7",
"role": "Developer",
"competency": "Kicks",
"employeeNumber": null,
"name": "Safeguarding Level 3"
}, {
"id": 1,
"username": "JJ",
"lastLoggedIn": null,
"archive": false,
"forename": "J",
"surname": "J",
"band": "Band 7",
"role": "Developer",
"competency": "Kicks",
"employeeNumber": null,
"name": "Safeguarding Level 2"
}, {
"id": 276,
"username": "IH",
"lastLoggedIn": null,
"archive": false,
"forename": "I",
"surname": "Hashmi",
"band": "Band 8",
"role": "Chap",
"competency": "Pie",
"employeeNumber": null,
"name": "Safeguarding Level 3"
}, {
"id": 276,
"username": "IH",
"lastLoggedIn": null,
"archive": false,
"forename": "I",
"surname": "Hashmi",
"band": "Band 8",
"role": "Chap",
"competency": "Pie",
"employeeNumber": null,
"name": "Safeguarding Level 2"
}, {
"id": 276,
"username": "IH",
"lastLoggedIn": null,
"archive": false,
"forename": "I",
"surname": "Hashmi",
"band": "Band 8",
"role": "Chap",
"competency": "Pie",
"employeeNumber": null,
"name": "Major Incident"
}, {
"id": 277,
"username": "LC",
"lastLoggedIn": null,
"archive": false,
"forename": "L",
"surname": "C",
"band": "Band 9",
"role": "Lady",
"competency": "Pizza",
"employeeNumber": null,
"name": "WMH ED Nursing"
}, {
"id": 277,
"username": "LC",
"lastLoggedIn": null,
"archive": false,
"forename": "L",
"surname": "C",
"band": "Band 9",
"role": "Lady",
"competency": "Pizza",
"employeeNumber": null,
"name": "Sedation"
}, {
"id": 277,
"username": "LC",
"lastLoggedIn": null,
"archive": false,
"forename": "L",
"surname": "C",
"band": "Band 9",
"role": "Lady",
"competency": "Pizza",
"employeeNumber": null,
"name": "Safeguarding Level 3"
}, {
"id": 281,
"username": "MJ",
"lastLoggedIn": null,
"archive": false,
"forename": "M",
"surname": "J",
"band": "Band 9",
"role": "Solutions Developer Manager",
"competency": "Ninja",
"employeeNumber": "23546329",
"name": "Digital Team"
}, {
"id": 283,
"username": "CG",
"lastLoggedIn": null,
"archive": false,
"forename": "C",
"surname": "G",
"band": "5",
"role": "Senior Solutions Developer",
"competency": "LOL",
"employeeNumber": "23546205",
"name": "WMH ED Medics"
}, {
"id": 283,
"username": "CG",
"lastLoggedIn": null,
"archive": false,
"forename": "C",
"surname": "G",
"band": "5",
"role": "Senior Solutions Developer",
"competency": "LOL",
"employeeNumber": "23546205",
"name": "WMH ED Nursing"
}, {
"id": 283,
"username": "CG",
"lastLoggedIn": null,
"archive": false,
"forename": "C",
"surname": "G",
"band": "5",
"role": "Senior Solutions Developer",
"competency": "LOL",
"employeeNumber": "23546205",
"name": "Sedation"
}, {
"id": 283,
"username": "CG",
"lastLoggedIn": null,
"archive": false,
"forename": "C",
"surname": "G",
"band": "5",
"role": "Senior Solutions Developer",
"competency": "LOL",
"employeeNumber": "23546205",
"name": "Safeguarding Level 3"
}, {
"id": 283,
"username": "CG",
"lastLoggedIn": null,
"archive": false,
"forename": "C",
"surname": "G",
"band": "5",
"role": "Senior Solutions Developer",
"competency": "LOL",
"employeeNumber": "23546205",
"name": "Safeguarding Level 2"
}, {
"id": 283,
"username": "CG",
"lastLoggedIn": null,
"archive": false,
"forename": "C",
"surname": "G",
"band": "5",
"role": "Senior Solutions Developer",
"competency": "LOL",
"employeeNumber": "23546205",
"name": "Major Incident"
}, {
"id": 284,
"username": "Bob",
"lastLoggedIn": null,
"archive": true,
"forename": "Bob",
"surname": "Bob",
"band": "Bob",
"role": "Bob",
"competency": "Bob",
"employeeNumber": "XXX",
"name": "WMH ED Medics"
}, {
"id": 284,
"username": "Bob",
"lastLoggedIn": null,
"archive": true,
"forename": "Bob",
"surname": "Bob",
"band": "Bob",
"role": "Bob",
"competency": "Bob",
"employeeNumber": "XXX",
"name": "WMH ED Nursing"
}, {
"id": 284,
"username": "Bob",
"lastLoggedIn": null,
"archive": true,
"forename": "Bob",
"surname": "Bob",
"band": "Bob",
"role": "Bob",
"competency": "Bob",
"employeeNumber": "XXX",
"name": "Sedation"
}, {
"id": 284,
"username": "Bob",
"lastLoggedIn": null,
"archive": true,
"forename": "Bob",
"surname": "Bob",
"band": "Bob",
"role": "Bob",
"competency": "Bob",
"employeeNumber": "XXX",
"name": "Safeguarding Level 3"
}, {
"id": 284,
"username": "Bob",
"lastLoggedIn": null,
"archive": true,
"forename": "Bob",
"surname": "Bob",
"band": "Bob",
"role": "Bob",
"competency": "Bob",
"employeeNumber": "XXX",
"name": "Safeguarding Level 2"
}, {
"id": 284,
"username": "Bob",
"lastLoggedIn": null,
"archive": true,
"forename": "Bob",
"surname": "Bob",
"band": "Bob",
"role": "Bob",
"competency": "Bob",
"employeeNumber": "XXX",
"name": "Major Incident"
}];
var newData = [];
baseData.forEach(function(item, index) {
if (newData.length === 0) {
newData.push(item);
} else {
var dIndex = -1;
newData.forEach(function(itm, idx) {
if (item.id === itm.id) dIndex = idx;
});
if (dIndex !== -1) {
var oldname = newData[dIndex].name;
if (typeof(oldname).toString() === 'string') {
newData[dIndex].name = [oldname, item.name];
}
} else {
newData.push(item);
}
}
});
console.log(newData);
jsFiddle: https://jsfiddle.net/jeqdbn12/
This groups/reduces the object by the key called "name".
However it seems to nest the values for key, so using the array above, for Bob, I get:
{
archive: true,
band: "Bob",
competency: "Bob",
employeeNumber: "XXX",
forename: "Bob",
id: 284,
lastLoggedIn: null,
name: [[[[["WMH ED Medics", "WMH ED Nursing"], "Sedation"], "Safeguarding Level 3"], "Safeguarding Level 2"], "Major Incident"],
role: "Bob",
surname: "Bob",
username: "Bob"
}
However, what I need is for the key called "name" to be an array of values or a comma delimited string. Any advice?
So I need something like:
...
name: ["value1","value2"]
...
Use Array.reduce
Logic
Loop through array.
Push the nodes to accumulator
Before pushing verify whether there is a node with same id and username already exist in the accumulator.
If yes, push the name of current node to the name array of that node
If not push the node to accumulator with name as a array with the value of current name as the first element.
const baseData = [{"id":1,"username":"JJ","lastLoggedIn":null,"archive":false,"forename":"J","surname":"J","band":"Band 7","role":"Developer","competency":"Kicks","employeeNumber":null,"name":"Sedation"},{"id":1,"username":"JJ","lastLoggedIn":null,"archive":false,"forename":"J","surname":"J","band":"Band 7","role":"Developer","competency":"Kicks","employeeNumber":null,"name":"Safeguarding Level 3"},{"id":1,"username":"JJ","lastLoggedIn":null,"archive":false,"forename":"J","surname":"J","band":"Band 7","role":"Developer","competency":"Kicks","employeeNumber":null,"name":"Safeguarding Level 2"},{"id":276,"username":"IH","lastLoggedIn":null,"archive":false,"forename":"I","surname":"Hashmi","band":"Band 8","role":"Chap","competency":"Pie","employeeNumber":null,"name":"Safeguarding Level 3"},{"id":276,"username":"IH","lastLoggedIn":null,"archive":false,"forename":"I","surname":"Hashmi","band":"Band 8","role":"Chap","competency":"Pie","employeeNumber":null,"name":"Safeguarding Level 2"},{"id":276,"username":"IH","lastLoggedIn":null,"archive":false,"forename":"I","surname":"Hashmi","band":"Band 8","role":"Chap","competency":"Pie","employeeNumber":null,"name":"Major Incident"},{"id":277,"username":"LC","lastLoggedIn":null,"archive":false,"forename":"L","surname":"C","band":"Band 9","role":"Lady","competency":"Pizza","employeeNumber":null,"name":"WMH ED Nursing"},{"id":277,"username":"LC","lastLoggedIn":null,"archive":false,"forename":"L","surname":"C","band":"Band 9","role":"Lady","competency":"Pizza","employeeNumber":null,"name":"Sedation"},{"id":277,"username":"LC","lastLoggedIn":null,"archive":false,"forename":"L","surname":"C","band":"Band 9","role":"Lady","competency":"Pizza","employeeNumber":null,"name":"Safeguarding Level 3"},{"id":281,"username":"MJ","lastLoggedIn":null,"archive":false,"forename":"M","surname":"J","band":"Band 9","role":"Solutions Developer Manager","competency":"Ninja","employeeNumber":"23546329","name":"Digital Team"},{"id":283,"username":"CG","lastLoggedIn":null,"archive":false,"forename":"C","surname":"G","band":"5","role":"Senior Solutions Developer","competency":"LOL","employeeNumber":"23546205","name":"WMH ED Medics"},{"id":283,"username":"CG","lastLoggedIn":null,"archive":false,"forename":"C","surname":"G","band":"5","role":"Senior Solutions Developer","competency":"LOL","employeeNumber":"23546205","name":"WMH ED Nursing"},{"id":283,"username":"CG","lastLoggedIn":null,"archive":false,"forename":"C","surname":"G","band":"5","role":"Senior Solutions Developer","competency":"LOL","employeeNumber":"23546205","name":"Sedation"},{"id":283,"username":"CG","lastLoggedIn":null,"archive":false,"forename":"C","surname":"G","band":"5","role":"Senior Solutions Developer","competency":"LOL","employeeNumber":"23546205","name":"Safeguarding Level 3"},{"id":283,"username":"CG","lastLoggedIn":null,"archive":false,"forename":"C","surname":"G","band":"5","role":"Senior Solutions Developer","competency":"LOL","employeeNumber":"23546205","name":"Safeguarding Level 2"},{"id":283,"username":"CG","lastLoggedIn":null,"archive":false,"forename":"C","surname":"G","band":"5","role":"Senior Solutions Developer","competency":"LOL","employeeNumber":"23546205","name":"Major Incident"},{"id":284,"username":"Bob","lastLoggedIn":null,"archive":true,"forename":"Bob","surname":"Bob","band":"Bob","role":"Bob","competency":"Bob","employeeNumber":"XXX","name":"WMH ED Medics"},{"id":284,"username":"Bob","lastLoggedIn":null,"archive":true,"forename":"Bob","surname":"Bob","band":"Bob","role":"Bob","competency":"Bob","employeeNumber":"XXX","name":"WMH ED Nursing"},{"id":284,"username":"Bob","lastLoggedIn":null,"archive":true,"forename":"Bob","surname":"Bob","band":"Bob","role":"Bob","competency":"Bob","employeeNumber":"XXX","name":"Sedation"},{"id":284,"username":"Bob","lastLoggedIn":null,"archive":true,"forename":"Bob","surname":"Bob","band":"Bob","role":"Bob","competency":"Bob","employeeNumber":"XXX","name":"Safeguarding Level 3"},{"id":284,"username":"Bob","lastLoggedIn":null,"archive":true,"forename":"Bob","surname":"Bob","band":"Bob","role":"Bob","competency":"Bob","employeeNumber":"XXX","name":"Safeguarding Level 2"},{"id":284,"username":"Bob","lastLoggedIn":null,"archive":true,"forename":"Bob","surname":"Bob","band":"Bob","role":"Bob","competency":"Bob","employeeNumber":"XXX","name":"Major Incident"}];
const newData = baseData.reduce((acc, curr) => {
const node = acc.find((item) => item.id === curr.id && item.username === curr.username);
if (node) {
node.name.push(curr.name);
} else {
const newNode = { ...curr };
newNode.name = [curr.name];
acc.push(newNode)
}
return acc;
}, []);
console.log(newData);
Related
I'm making a fetch to the API to grab the total number of players, the returned data is below:
"data": [
{
"id": 558,
"first_name": "Dave",
"height_feet": null,
"height_inches": null,
"last_name": "Jamerson",
"position": "",
"team": {
"id": 11,
"abbreviation": "HOU",
"city": "Houston",
"conference": "West",
"division": "Southwest",
"full_name": "Houston Rockets",
"name": "Rockets"
},
"weight_pounds": null
},
{
"id": 559,
"first_name": "Scott",
"height_feet": null,
"height_inches": null,
"last_name": "Brooks",
"position": "",
"team": {
"id": 18,
"abbreviation": "MIN",
"city": "Minnesota",
"conference": "West",
"division": "Northwest",
"full_name": "Minnesota Timberwolves",
"name": "Timberwolves"
},
"weight_pounds": null
},
{
"id": 560,
"first_name": "Rolando",
"height_feet": null,
"height_inches": null,
"last_name": "Blackman",
"position": "",
"team": {
"id": 7,
"abbreviation": "DAL",
"city": "Dallas",
"conference": "West",
"division": "Southwest",
"full_name": "Dallas Mavericks",
"name": "Mavericks"
},
"weight_pounds": null
},
{
"id": 561,
"first_name": "Avery",
"height_feet": null,
"height_inches": null,
"last_name": "Johnson",
"position": "",
"team": {
"id": 8,
"abbreviation": "DEN",
"city": "Denver",
"conference": "West",
"division": "Northwest",
"full_name": "Denver Nuggets",
"name": "Nuggets"
},
"weight_pounds": null
},
{
"id": 562,
"first_name": "Rod",
"height_feet": null,
"height_inches": null,
"last_name": "Higgins",
"position": "",
"team": {
"id": 10,
"abbreviation": "GSW",
"city": "Golden State",
"conference": "West",
"division": "Pacific",
"full_name": "Golden State Warriors",
"name": "Warriors"
},
"weight_pounds": null
},
{
"id": 566,
"first_name": "Sam",
"height_feet": null,
"height_inches": null,
"last_name": "Vincent",
"position": "",
"team": {
"id": 22,
"abbreviation": "ORL",
"city": "Orlando",
"conference": "East",
"division": "Southeast",
"full_name": "Orlando Magic",
"name": "Magic"
},
"weight_pounds": null
},
{
"id": 567,
"first_name": "Isiah",
"height_feet": null,
"height_inches": null,
"last_name": "Thomas",
"position": "",
"team": {
"id": 9,
"abbreviation": "DET",
"city": "Detroit",
"conference": "East",
"division": "Central",
"full_name": "Detroit Pistons",
"name": "Pistons"
},
"weight_pounds": null
}
],
"meta": {
"total_pages": 39,
"current_page": 1,
"next_page": 2,
"per_page": 100,
"total_count": 3828
}
Problem is, the API only allows for a max of 100 results. As you can see in the "meta" object at the bottom, there is a total_count of 3828 and 39 pages, how would I access the other pages and get all 3828 results? currently I only get the first page with 100 results when I fetch.
const getPlayers = () => {
fetch(`https://www.balldontlie.io/api/v1/players?per_page=100`)
.then(res => res.json())
.then(data => dispatch(playerActions.getPlayers(data)));
}
I'm comparing two JSON and returning mismatched values from the second JSON with a key and storing them in a new JSON object. I'm using a loop for comparing two JSON objects. If there is more than one difference, it is overwritten by the previous object stored. Can you please tell me how to resolve it?
var finalOutputJson;
let jsonOutput1 = {},
jsonOutput2 = {};
for (let i of jsonData1) {
var d = jsonData2.filter(
(data) => data.Unique == i.Unique
);
if (d != []) {
let keys = Object.keys(i);
for (let j of keys) {
if (j == "Title") {
if (i[j] != d[0]["Name"].Title) {
jsonOutput1 = { [j]: d[0]["Name"].Title };
console.log(jsonOutput1);
}
} else if (i[j] != d[0][j]) {
jsonOutput2 = { [j]: d[0][j] };
console.log(jsonOutput2);
}
[![jsonOutput1][1]][1]
inputJsonData1 :-
[
{
"Title": "Ankit Singh1",
"Aadhar_x0020_Nuber": 999941057058,
"Age": 54,
"Gender": "M",
"Phone": 2810806979,
"Father_x0020_Name": "Mr. Choudhury",
"Email": "sschoudhury#dummyemail.com",
"City": "New Delhi",
"State": "New delhi"
},
{
"Title": "Munendra",
"Aadhar_x0020_Nuber": 999971658847,
"Age": 44,
"Gender": "M",
"Phone": 2314475929,
"Father_x0020_Name": "Mr. Agarwal",
"Email": "kma#mailserver.com",
"City": "Udaipur",
"State": "Rajasthan"
},
{
"Title": "Mayank Sharma3",
"Aadhar_x0020_Nuber": 999933119405,
"Age": 79,
"Gender": "F",
"Phone": 2837032088,
"Father_x0020_Name": "Mr. Bedi",
"Email": "bedi2020#mailserver.com",
"City": "Bareilly",
"State": "Uttar Pradesh"
},
{
"Title": "Shikhar Saxena5",
"Aadhar_x0020_Nuber": 999955183433,
"Age": 37,
"Gender": "M",
"Phone": 2821096353,
"Father_x0020_Name": "Mr. Pandey",
"Email": "rpandey#mailserver.com",
"City": "Mumbai",
"State": "Maharastra"
},
{
"Title": "Namita Rastogi",
"Aadhar_x0020_Nuber": 999990501894,
"Age": 40,
"Gender": "F",
"Phone": 2821096350,
"Father_x0020_Name": "Mr. Kapoor",
"Email": "anisha#gmail.com",
"City": "Bangalore",
"State": "Karnataka"
}
]
inputJsonData2:-
[
{
"odata.type": "SP.Data.Aadhar_x005f_DataListItem",
"odata.id": "1a8bda26-b339-4e95-a42e-cf8b15aa571c",
"odata.etag": "\"3\"",
"odata.editLink": "Web/Lists(guid'f57b8a37-9a12-48fa-ae4a-f2c360611fd2')/Items(1)",
"Name#odata.navigationLinkUrl": "Web/Lists(guid'f57b8a37-9a12-48fa-ae4a-f2c360611fd2')/Items(1)/Name",
"Name": {
"odata.type": "SP.Data.UserInfoItem",
"odata.id": "e0fc5839-8528-460f-9a97-735b3e676870",
"Title": "Ankit Singh"
},
"FileSystemObjectType": 0,
"Id": 1,
"ServerRedirectedEmbedUri": null,
"ServerRedirectedEmbedUrl": "",
"ID": 1,
"ContentTypeId": "0x0100CAEC71E55732C14BA36095D2FA8E6CA80037B4C7B1E7CFFA4D83E55D30CD77AC99",
"Title": null,
"Modified": "2022-06-07T09:31:14Z",
"Created": "2022-06-02T08:15:05Z",
"AuthorId": 1282,
"EditorId": 1282,
"OData__UIVersionString": "3.0",
"Attachments": false,
"GUID": "c27f786a-8599-400c-b284-f7bec308a861",
"ComplianceAssetId": null,
"Aadhar_x0020_Nuber": 999941057058,
"NameId": 1282,
"NameStringId": "1282",
"Age": 54,
"Gender": "M",
"Phone": 2810806979,
"Father_x0020_Name": "Mr. Choudhury",
"Email": "sschoudhury#dummyemail.com",
"City": "New Delhi",
"State": "New delhi"
},
{
"odata.type": "SP.Data.Aadhar_x005f_DataListItem",
"odata.id": "bbb3d19c-5fae-463b-993a-08e3665f4997",
"odata.etag": "\"2\"",
"odata.editLink": "Web/Lists(guid'f57b8a37-9a12-48fa-ae4a-f2c360611fd2')/Items(2)",
"Name#odata.navigationLinkUrl": "Web/Lists(guid'f57b8a37-9a12-48fa-ae4a-f2c360611fd2')/Items(2)/Name",
"Name": {
"odata.type": "SP.Data.UserInfoItem",
"odata.id": "f9f74da9-4aba-4cdc-8e4f-2985883c4602",
"Title": "Munendra"
},
"FileSystemObjectType": 0,
"Id": 2,
"ServerRedirectedEmbedUri": null,
"ServerRedirectedEmbedUrl": "",
"ID": 2,
"ContentTypeId": "0x0100CAEC71E55732C14BA36095D2FA8E6CA80037B4C7B1E7CFFA4D83E55D30CD77AC99",
"Title": null,
"Modified": "2022-06-16T06:40:16Z",
"Created": "2022-06-02T08:17:48Z",
"AuthorId": 1282,
"EditorId": 1282,
"OData__UIVersionString": "2.0",
"Attachments": false,
"GUID": "afb84bd6-0253-4bf8-a515-824b13ae1a4e",
"ComplianceAssetId": null,
"Aadhar_x0020_Nuber": 999971658847,
"NameId": 107,
"NameStringId": "107",
"Age": 44,
"Gender": "F",
"Phone": 2314475929,
"Father_x0020_Name": "Mr. Agarwal",
"Email": "kma#mailserver.com",
"City": "Udaipur",
"State": "Rajasthan"
},
{
"odata.type": "SP.Data.Aadhar_x005f_DataListItem",
"odata.id": "49217d7e-200c-496b-b728-bd5307bbe1db",
"odata.etag": "\"1\"",
"odata.editLink": "Web/Lists(guid'f57b8a37-9a12-48fa-ae4a-f2c360611fd2')/Items(3)",
"Name#odata.navigationLinkUrl": "Web/Lists(guid'f57b8a37-9a12-48fa-ae4a-f2c360611fd2')/Items(3)/Name",
"Name": {
"odata.type": "SP.Data.UserInfoItem",
"odata.id": "df5b1286-0009-48ba-a7bc-7b269a9ebcf7",
"Title": "Mayank Sharma3"
},
"FileSystemObjectType": 0,
"Id": 3,
"ServerRedirectedEmbedUri": null,
"ServerRedirectedEmbedUrl": "",
"ID": 3,
"ContentTypeId": "0x0100CAEC71E55732C14BA36095D2FA8E6CA80037B4C7B1E7CFFA4D83E55D30CD77AC99",
"Title": null,
"Modified": "2022-06-02T08:18:58Z",
"Created": "2022-06-02T08:18:58Z",
"AuthorId": 1282,
"EditorId": 1282,
"OData__UIVersionString": "1.0",
"Attachments": false,
"GUID": "e5ac3af3-e024-4703-b946-96cdc2ff3b5e",
"ComplianceAssetId": null,
"Aadhar_x0020_Nuber": 999933119405,
"NameId": 705,
"NameStringId": "705",
"Age": 79,
"Gender": "M",
"Phone": 2837032088,
"Father_x0020_Name": "Mr. Bedi",
"Email": "bedi2020#mailserver.com",
"City": "Bareilly",
"State": "Uttar Pradesh"
},
{
"odata.type": "SP.Data.Aadhar_x005f_DataListItem",
"odata.id": "e1f7ffaa-1bc7-4afa-bccb-18965f13c750",
"odata.etag": "\"1\"",
"odata.editLink": "Web/Lists(guid'f57b8a37-9a12-48fa-ae4a-f2c360611fd2')/Items(4)",
"Name#odata.navigationLinkUrl": "Web/Lists(guid'f57b8a37-9a12-48fa-ae4a-f2c360611fd2')/Items(4)/Name",
"Name": {
"odata.type": "SP.Data.UserInfoItem",
"odata.id": "5f9e7c49-9bde-4b66-90c8-12e156db60ce",
"Title": "Shikhar Saxena"
},
"FileSystemObjectType": 0,
"Id": 4,
"ServerRedirectedEmbedUri": null,
"ServerRedirectedEmbedUrl": "",
"ID": 4,
"ContentTypeId": "0x0100CAEC71E55732C14BA36095D2FA8E6CA80037B4C7B1E7CFFA4D83E55D30CD77AC99",
"Title": null,
"Modified": "2022-06-02T08:20:16Z",
"Created": "2022-06-02T08:20:16Z",
"AuthorId": 1282,
"EditorId": 1282,
"OData__UIVersionString": "1.0",
"Attachments": false,
"GUID": "c3395b35-a981-4754-abb5-8b4b0fe65a51",
"ComplianceAssetId": null,
"Aadhar_x0020_Nuber": 999955183433,
"NameId": 846,
"NameStringId": "846",
"Age": 37,
"Gender": "M",
"Phone": 2821096353,
"Father_x0020_Name": "Mr. Pandey",
"Email": "rpandey#mailserver.com",
"City": "Mumbai",
"State": "Maharastra"
},
{
"odata.type": "SP.Data.Aadhar_x005f_DataListItem",
"odata.id": "29ba7f05-52e8-4ded-b3e5-22a9b3001d97",
"odata.etag": "\"2\"",
"odata.editLink": "Web/Lists(guid'f57b8a37-9a12-48fa-ae4a-f2c360611fd2')/Items(5)",
"Name#odata.navigationLinkUrl": "Web/Lists(guid'f57b8a37-9a12-48fa-ae4a-f2c360611fd2')/Items(5)/Name",
"Name": {
"odata.type": "SP.Data.UserInfoItem",
"odata.id": "cdce69d7-a601-4c9f-ad03-0a85eccb510b",
"Title": "Namita Rastogi"
},
"FileSystemObjectType": 0,
"Id": 5,
"ServerRedirectedEmbedUri": null,
"ServerRedirectedEmbedUrl": "",
"ID": 5,
"ContentTypeId": "0x0100CAEC71E55732C14BA36095D2FA8E6CA80037B4C7B1E7CFFA4D83E55D30CD77AC99",
"Title": null,
"Modified": "2022-06-09T07:25:17Z",
"Created": "2022-06-02T08:21:46Z",
"AuthorId": 1282,
"EditorId": 1282,
"OData__UIVersionString": "2.0",
"Attachments": false,
"GUID": "7a7d7906-442e-41eb-9f17-16f729fbb310",
"ComplianceAssetId": null,
"Aadhar_x0020_Nuber": 999990501894,
"NameId": 849,
"NameStringId": "849",
"Age": 40,
"Gender": "F",
"Phone": 2821096351,
"Father_x0020_Name": "Mr. Kapoor",
"Email": "anisha#gmail.com",
"City": "Bangalore",
"State": "Karnataka"
}
]
Expected Output:-
[
{
"Title": "Ankit Singh",
"Aadhar_x0020_Nuber": ,
"Age": ,
"Gender": "",
"Phone": ,
"Father_x0020_Name": "",
"Email": "",
"City": "",
"State": ""
},
{
"Title": "",
"Aadhar_x0020_Nuber": ,
"Age": ,
"Gender": "F",
"Phone": ,
"Father_x0020_Name": "",
"Email": "",
"City": "",
"State": ""
},
{
"Title": "",
"Aadhar_x0020_Nuber": ,
"Age": ,
"Gender": "M",
"Phone": ,
"Father_x0020_Name": "",
"Email": "",
"City": "",
"State": ""
},
{
"Title": "Shikhar Saxena",
"Aadhar_x0020_Nuber": ,
"Age": ,
"Gender": "",
"Phone": ,
"Father_x0020_Name": "",
"Email": "",
"City": "",
"State": ""
},
{
"Title": "",
"Aadhar_x0020_Nuber": ,
"Age": ,
"Gender": "",
"Phone": 2821096351,
"Father_x0020_Name": "",
"Email": "",
"City": "",
"State": ""
}
]
You are assigning a new value to the jsonOutput1 and jsonOutput2 everytime. You want to add to the existing object, in that case you need to change the lines:
jsonOutput1 = { [j]: d[0]["Name"].Title };
to
jsonOutput1[j] = d[0]["Name"].Title;
Similarly update the code for your jsonOutput2 line as well.
You can refer to this MDN JavaScript Guide Documentation and refer to the part where there demonstrate how to create additional properties in an existing object.
Hopefully, this will work.
This question already has answers here:
Remove property for all objects in array
(18 answers)
Closed 1 year ago.
I have the following object in my code.
[
{
"Name": "John Johnsson",
"Adress": "Linkoping",
"Id": 0,
"Age": "43",
"Role": "Software Engineer"
},
{
"Name": "Marcus Svensson",
"Adress": "Norrköping",
"Age": "26",
"Id": 1,
"Role": "Project Manager"
},
{
"Age": "25",
"Name": "Trevor McNoah",
"Id": 2,
"Adress": "Stockholm",
"Role": "CTO"
}
]
How do I best delete all the "Adress" fields? So I end up with the following result. I've been struggling to find an answer to this basic question.
[
{
"Name": "John Johnsson",
"Id": 0,
"Age": "43",
"Role": "Software Engineer"
},
{
"Name": "Marcus Svensson",
"Age": "26",
"Id": 1,
"Role": "Project Manager"
},
{
"Age": "25",
"Name": "Trevor McNoah",
"Id": 2,
"Role": "CTO"
}
]
JavaScript has delete operator:
data.forEach(item => {
delete item['Address'];
})
You can read more about delete operator here.
You can do it like that: so you don't mutate the initial array.
const listWithoutAddress = list.map(({Adress, ...rest}) => ({...rest}));
To remove a property from a js object you can use the delete command, like this:
let arr = [
{
"Name": "John Johnsson",
"Address": "Linkoping",
"Id": 0,
"Age": "43",
"Role": "Software Engineer"
},
{
"Name": "Marcus Svensson",
"Address": "Norrköping",
"Age": "26",
"Id": 1,
"Role": "Project Manager"
},
{
"Age": "25",
"Name": "Trevor McNoah",
"Id": 2,
"Address": "Stockholm",
"Role": "CTO"
}
];
console.log(arr);
arr.forEach(_=>{delete _.Address}); // <-- this is it
console.log(arr);
Im trying to filter all the contact I have with an array of numbers I want to remove. This should loop on every contact and remove the numbers not needed. Some contacts have two numbers and only one could be deleted but not the hole contact. I tried already to filter and see if the selected number is in an index but the forEach doesn't seem to be working any advice?. I don't think forEach returns something
const filteredContacts = contacts.filter(contact => numbers.indexOf(contact.phoneNumbers.forEach(phone => phone.number)) > -1);
//2 sample of all contacts
// contacts
Object {
"company": "Financial Services Inc.",
"contactType": "person",
"firstName": "Hank",
"id": "2E73EE73-C03F-4D5F-B1E8-44E85A70F170",
"imageAvailable": false,
"jobTitle": "Portfolio Manager",
"lastName": "Zakroff",
"middleName": "M.",
"name": "Hank M. Zakroff",
"phoneNumbers": Array [
Object {
"countryCode": "us",
"digits": "5557664823",
"id": "337A78CC-C90A-46AF-8D4B-6CC43251AD1A",
"label": "work",
"number": "(555) 766-4823",
},
Object {
"countryCode": "us",
"digits": "7075551854",
"id": "E998F7A3-CC3C-4CF1-BC21-A53682BC7C7A",
"label": "other",
"number": "(707) 555-1854",
},
],
},
Object {
"contactType": "person",
"firstName": "David",
"id": "E94CD15C-7964-4A9B-8AC4-10D7CFB791FD",
"imageAvailable": false,
"lastName": "Taylor",
"name": "David Taylor",
"phoneNumbers": Array [
Object {
"countryCode": "us",
"digits": "5556106679",
"id": "FE064E55-C246-45F0-9C48-822BF65B943F",
"label": "home",
"number": "555-610-6679",
},
],
},
]
//numbers not to have
numbers = [
5557664823,
1344043005,
5467865467,
]
//Expected
Object {
"company": "Financial Services Inc.",
"contactType": "person",
"firstName": "Hank",
"id": "2E73EE73-C03F-4D5F-B1E8-44E85A70F170",
"imageAvailable": false,
"jobTitle": "Portfolio Manager",
"lastName": "Zakroff",
"middleName": "M.",
"name": "Hank M. Zakroff",
"phoneNumbers": Array [
Object {
"countryCode": "us",
"digits": "7075551854",
"id": "E998F7A3-CC3C-4CF1-BC21-A53682BC7C7A",
"label": "other",
"number": "(707) 555-1854",
},
],
},
Object {
"contactType": "person",
"firstName": "David",
"id": "E94CD15C-7964-4A9B-8AC4-10D7CFB791FD",
"imageAvailable": false,
"lastName": "Taylor",
"name": "David Taylor",
"phoneNumbers": Array [
Object {
"countryCode": "us",
"digits": "5556106679",
"id": "FE064E55-C246-45F0-9C48-822BF65B943F",
"label": "home",
"number": "555-610-6679",
},
],
},
]
If I understood right, you want to filter out some numbers from the contact's phone numbers. One solution to this can be approached using Array.map() over the contacts array and replace the phoneNumbers array with a new filtered array. For the filtering procedure we can use Array.filter() and Array.includes() to check if a phone number belong to the list of numbers you want to filter out.
const contacts = [
{
"company": "Financial Services Inc.",
"contactType": "person",
"firstName": "Hank",
"id": "2E73EE73-C03F-4D5F-B1E8-44E85A70F170",
"imageAvailable": false,
"jobTitle": "Portfolio Manager",
"lastName": "Zakroff",
"middleName": "M.",
"name": "Hank M. Zakroff",
"phoneNumbers": [
{
"countryCode": "us",
"digits": "5557664823",
"id": "337A78CC-C90A-46AF-8D4B-6CC43251AD1A",
"label": "work",
"number": "(555) 766-4823",
},
{
"countryCode": "us",
"digits": "7075551854",
"id": "E998F7A3-CC3C-4CF1-BC21-A53682BC7C7A",
"label": "other",
"number": "(707) 555-1854",
},
],
},
{
"contactType": "person",
"firstName": "David",
"id": "E94CD15C-7964-4A9B-8AC4-10D7CFB791FD",
"imageAvailable": false,
"lastName": "Taylor",
"name": "David Taylor",
"phoneNumbers": [
{
"countryCode": "us",
"digits": "5556106679",
"id": "FE064E55-C246-45F0-9C48-822BF65B943F",
"label": "home",
"number": "555-610-6679",
},
],
},
];
// Numbers to filter out.
const numsToFilterOut = [5557664823];
let res = contacts.map(contact =>
{
contact.phoneNumbers = contact.phoneNumbers.filter(
// Unary plus is used to coerces (cast) the string to number.
phone => !numsToFilterOut.includes(+phone.digits)
);
return contact;
});
console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 6 years ago.
I can't seem to console.log the item name and just that. It's in the position data -> pricing -> tables -> items -> name. I am going for an output that says "Toy Panda".
[
{
"event": "recipient_completed",
"data": {
"id": "msFYActMfJHqNTKH8YSvF1",
"name": "Sample Document",
"status": "document.draft",
"date_created": "2014-10-06T08:42:13.836022Z",
"date_modified": "2016-03-04T02:21:13.963750Z",
"action_date": "2016-09-02T22:26:52.227554",
"action_by": {
"id": "FyXaS4SlT2FY7uLPqKD9f2",
"email": "john#appleseed.com",
"first_name": "John",
"last_name": "Appleseed"
},
"created_by": {
"id": "FyXaS4SlT2FY7uLPqKD9f2",
"email": "john#appleseed.com",
"first_name": "John",
"last_name": "Appleseed",
"avatar": "https://pd-live-media.s3.amazonaws.com/users/FyXaS4SlT2FY7uLPqKD9f2/avatar.jpg"
},
"recipients": [
{
"id": "FyXaS4SlT2FY7uLPqKD9f2",
"email": "john#appleseed.com",
"first_name": "John",
"last_name": "Appleseed",
"role": "signer",
"recipient_type": "Signer",
"has_completed": true
}
],
"sent_by": {
"id": "FyXaS4SlT2FY7uLPqKD9f2",
"email": "john#appleseed.com",
"first_name": "John",
"last_name": "Appleseed",
"avatar": "https://pd-live-media.s3.amazonaws.com/users/FyXaS4SlT2FY7uLPqKD9f2/avatar.jpg"
},
"metadata": {
"salesforce_opp_id": "123456",
"my_favorite_pet": "Panda"
},
"tokens": [
{
"name": "Favorite Animal",
"value": "Panda"
}
],
"fields": [
{
"uuid": "YcLBNUKcx45UFxAK3NjLIH",
"name": "Textfield",
"title": "Favorite Animal",
"value": "Panda",
"assigned_to": {
"id": "FyXaS4SlT2FY7uLPqKD9f2",
"email": "john#appleseed.com",
"first_name": "John",
"last_name": "Appleseed",
"role": "Signer",
"recipient_type": "signer",
"has_completed": true,
"type": "recipient"
}
}
],
"pricing": {
"tables": [
{
"id": 82307036,
"name": "PricingTable1",
"is_included_in_total": true,
"summary": {
"discount": 10,
"tax": 0,
"total": 60,
"subtotal": 60
},
"items": [
{
"id": "4ElJ4FEsG4PHAVNPR5qoo9",
"qty": 1,
"name": "Toy Panda",
"cost": "25",
"price": "53",
"description": "Buy a Panda",
"custom_fields": {
"sampleField": "Sample Field"
},
"custom_columns": {
"sampleColumn": "Sample Column"
},
"discount": 10,
"subtotal": 60
}
],
"total": 60
}
]
},
"tags": [
"test tag",
"sales",
"support"
]
}
}
]
I would really appreciate a tip. Thank you
If you store your JSON object into variable called obj you can access that value ("Toy Panda") with:
obj.data.pricing.tables[0].items[0].name
because tables and items are arrays.
Here is a sample example :
<script>
var data = '{"name": "mkyong","age": 30,"address": {"streetAddress": "88 8nd Street","city": "New York"},"phoneNumber": [{"type": "home","number": "111 111-1111"},{"type": "fax","number": "222 222-2222"}]}';
var json = JSON.parse(data);
alert(json["name"]); //mkyong
alert(json.name); //mkyong
</script>
Refer this link :[https://www.mkyong.com/javascript/how-to-access-json-object-in-javascript/][1]
In your case it should be something like :
var data = // your json;
var json = JSON.parse(data);
console.log(json.pricing.tables.items[0].name;