I currently use a function to take some values from one set to another, this works with me swapping across a positionTitle and startDate, a string and a date
But now I am trying to swap across the endDate but this data can be null, and I assume that's why it doesn't work. Also when I try to use a function checking for a null value, I get an error saying cant read endDate of undefined.
My original set of data looks like this:
individualsData = [{
"account": {
"id": "001b000003WnPy1AAF",
"fullName": "Adnan A. Khan"
},
"positions": [{
"id": "a16b0000004AxeBAAS",
"organizationId": "001b0000005gxmlAAA",
"organizationName": "Accenture",
"positionTitle": "Senior Manager, Energy",
"positionLevel": "5-Middle Management & Advisers",
"isPrimary": true,
"startDate": "2016-10-07",
"endDate": null
}]
}
The data set I want to manipulate looks like this:
graphData = {
"id": "sdsds",
"name": "sdsdsd",
"engagementAreas": [{
"id": "a6q0X000000IbqjQAC",
"name": "Centres",
"iconName": "c4ir",
"engagementTypes": [{
"id": "01tb0000007PY7RAAW",
"name": "4IR Centre Partnership",
"description": "<p><span style=\"color: rgb(23, 43, 77); font-size: 9pt;\">asdasdasdasdasds:</span></p><ul><li><span style=\"color: rgb(23, 43, 77); font-size: 9pt;\">A space for global cooperation</span></li></ul><p class=\"ql-indent-1\"><span style=\"color: rgb(23, 43, 77); font-size: 9pt;\">The Centre is dedicated to developing policy frameworks and governance protocols that accelerate the application of science and technology in the global public interest.</span></p><ul><li><span style=\"color: rgb(23, 43, 77); font-size: 9pt;\">A “do-tank”</span></li></ul><p class=\"ql-indent-1\"><span style=\"color: rgb(23, 43, 77); font-size: 9pt;\">Partner socialsand companies will co-design and pilot these frameworks and protocols for rapid iteration and scale. The Centre is not a think-tank, but rather a “do-tank</span><span style=\"color: rgb(23, 43, 77); font-size: 12px;\">”</span><span style=\"color: rgb(23, 43, 77); font-size: 9pt;\">.</span></p><ul><li><span style=\"color: rgb(23, 43, 77); font-size: 9pt;\">A champion for ethics and values in technology</span></li></ul><p class=\"ql-indent-1\"><span style=\"color: rgb(23, 43, 77); font-size: 9pt;\">All policy principles, frameworks and regulation developed at the Centre will prioritize ethics and values.</span></p>",
"hasActiveContract": true,
"engagements": [{
"id": "efefe",
"name": "Artificial Intelligence and Machine Learning",
"startDate": null,
"endDate": null,
"country": null,
"city": null,
"type": "Forum Community",
"members": [{
"id": "a0g0X00001juqkvQAA",
"account": {
"id": "001b0000002mX7cAAE",
"fullName": "PonyJeff"
},
"badgeType": null,
"endDate": null,
"engagementRole": null,
"position": {
"id": "a160X000004fOfuQAE",
"organizationId": "001b0000005gxmlAAA",
"organizationName": "efwefefwef",
"positionTitle": "Senior Managing Director, dereltict pony, Sustainability",
"positionLevel": "4-Head of statttt Unit/Head of Region",
"isPrimary": true,
"startDate": "2018-09-08",
"endDate": null
},
"startDate": "2019-02-15",
"status": "Active",
"statusLabel": "Active"
}
I have users with many different positions and start/end dates and position titles, my goal was to take the primary position from the first data set and replace all of the titles, start and end dates in the second data set, it works up until the endDate?
The logic I use:
const accountStartDate = individualsData
.reduce((current, item) => {
const primaryPosition = item.positions.find(p => p.isPrimary);
if (!current[item.account.fullName] || primaryPosition)
current[item.account.fullName] =
(primaryPosition && primaryPosition.startDate) ||
item.positions[0].startDate;
return current;
}, {});
const accountIdToPositionDict = individualsData
.reduce((current, item) => {
const primaryPosition = item.positions.find(p => p.isPrimary);
if (!current[item.account.fullName] || primaryPosition)
current[item.account.fullName] =
(primaryPosition && primaryPosition.title) ||
item.positions[0].positionTitle;
return current;
}, {});
const accountEndDate = individualsData
.reduce((current, item) => {
const primaryPosition = item.positions.find(p => p.isPrimary);
if (!current[item.account.fullName] || primaryPosition)
current[item.account.fullName] =
(primaryPosition && primaryPosition.endDate) ||
item.positions[0].endDate;
if (primaryPosition.endDate === null)
item.positions[0].endDate = null
return current;
}, {});
const updatedGraphTable = {
...graphData,
engagementAreas: graphData.engagementAreas.map(area => ({
...area,
engagementTypes: area.engagementTypes.map(type => ({
...type,
engagements: type.engagements.map(engagement => ({
...engagement,
members: engagement.members.map(member => ({
...member,
position: {
...member.position,
// use the found positionTitle, or the original one that was given
positionTitle: accountIdToPositionDict[member.account.fullName] || member.position.positionTitle,
startDate: accountStartDate[member.account.fullName] || member.position.startDate,
endDate: accountEndDate[member.account.fullName] || member.position.endDate
}
}))
}))
}))
}))
};
So the endDate doesn't work it doesn't carry over the null value, any help would be greatly appreciated.
const individualsData = [{
"account": {
"id": "001b000003WnPy1AAF",
"fullName": "Adnan A. Khan"
},
"positions": [{
"id": "a16b0000004AxeBAAS",
"organizationId": "001b0000005gxmlAAA",
"organizationName": "Accenture",
"positionTitle": "Senior Manager, Energy",
"positionLevel": "5-Middle Management & Advisers",
"isPrimary": true,
"startDate": "2016-10-07",
"endDate": null
}]
}];
const graphData = {
"id": "sdsds",
"name": "sdsdsd",
"engagementAreas": [{
"id": "a6q0X000000IbqjQAC",
"name": "Centres",
"iconName": "c4ir",
"engagementTypes": [{
"id": "01tb0000007PY7RAAW",
"name": "4IR Centre Partnership",
"description": "<p><span style=\"color: rgb(23, 43, 77); font-size: 9pt;\">asdasdasdasdasds:</span></p><ul><li><span style=\"color: rgb(23, 43, 77); font-size: 9pt;\">A space for global cooperation</span></li></ul><p class=\"ql-indent-1\"><span style=\"color: rgb(23, 43, 77); font-size: 9pt;\">The Centre is dedicated to developing policy frameworks and governance protocols that accelerate the application of science and technology in the global public interest.</span></p><ul><li><span style=\"color: rgb(23, 43, 77); font-size: 9pt;\">A “do-tank”</span></li></ul><p class=\"ql-indent-1\"><span style=\"color: rgb(23, 43, 77); font-size: 9pt;\">Partner socialsand companies will co-design and pilot these frameworks and protocols for rapid iteration and scale. The Centre is not a think-tank, but rather a “do-tank</span><span style=\"color: rgb(23, 43, 77); font-size: 12px;\">”</span><span style=\"color: rgb(23, 43, 77); font-size: 9pt;\">.</span></p><ul><li><span style=\"color: rgb(23, 43, 77); font-size: 9pt;\">A champion for ethics and values in technology</span></li></ul><p class=\"ql-indent-1\"><span style=\"color: rgb(23, 43, 77); font-size: 9pt;\">All policy principles, frameworks and regulation developed at the Centre will prioritize ethics and values.</span></p>",
"hasActiveContract": true,
"engagements": [{
"id": "efefe",
"name": "Artificial Intelligence and Machine Learning",
"startDate": null,
"endDate": null,
"country": null,
"city": null,
"type": "Forum Community",
"members": [{
"id": "a0g0X00001juqkvQAA",
"account": {
"id": "001b0000002mX7cAAE",
"fullName": "PonyJeff"
},
"badgeType": null,
"endDate": null,
"engagementRole": null,
"position": {
"id": "a160X000004fOfuQAE",
"organizationId": "001b0000005gxmlAAA",
"organizationName": "efwefefwef",
"positionTitle": "Senior Managing Director, dereltict pony, Sustainability",
"positionLevel": "4-Head of statttt Unit/Head of Region",
"isPrimary": true,
"startDate": "2018-09-08",
"endDate": null
},
"startDate": "2019-02-15",
"status": "Active",
"statusLabel": "Active"
}]
}]
}]
}]
};
const accountStartDate = individualsData
.reduce((current, item) => {
const primaryPosition = item.positions.find(p => p.isPrimary);
if (!current[item.account.fullName] || primaryPosition)
current[item.account.fullName] =
(primaryPosition && primaryPosition.startDate) ||
item.positions[0].startDate;
return current;
}, {});
const accountIdToPositionDict = individualsData
.reduce((current, item) => {
const primaryPosition = item.positions.find(p => p.isPrimary);
if (!current[item.account.fullName] || primaryPosition)
current[item.account.fullName] =
(primaryPosition && primaryPosition.title) ||
item.positions[0].positionTitle;
return current;
}, {});
const accountEndDate = individualsData
.reduce((current, item) => {
const primaryPosition = item.positions.find(p => p.isPrimary);
if (!current[item.account.fullName] || primaryPosition)
current[item.account.fullName] =
(primaryPosition && primaryPosition.endDate) ||
item.positions[0].endDate;
if (primaryPosition.endDate === null)
item.positions[0].endDate = null
return current;
}, {});
const updatedGraphTable = {
...graphData,
engagementAreas: graphData.engagementAreas.map(area => ({
...area,
engagementTypes: area.engagementTypes.map(type => ({
...type,
engagements: type.engagements.map(engagement => ({
...engagement,
members: engagement.members.map(member => ({
...member,
position: {
...member.position,
// use the found positionTitle, or the original one that was given
positionTitle: accountIdToPositionDict[member.account.fullName] || member.position.positionTitle,
startDate: accountStartDate[member.account.fullName] || member.position.startDate,
endDate: accountEndDate[member.account.fullName] || member.position.endDate
}
}))
}))
}))
}))
};
console.log(updatedGraphTable);