我目前正在使用一个函数将一组值从另一个值中取出来,这与我在positionTitle
和startDate
,字符串和日期之间进行交换
但是现在我试图在endDate
之间交换数据,但是该数据可以是null
,我认为这就是为什么它不起作用的原因。另外,当我尝试使用检查null
值的函数时,我收到一条错误消息,提示无法读取endDate of undefined
。
我原始的数据集如下:
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
}]
}
我要操作的数据集如下:
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"
}
我的用户具有很多不同的职位,开始/结束日期和职位名称,我的目标是从第一个数据集中获得主要职位,并替换第二个数据集中的所有标题,开始和结束日期,工作到endDate
为止?
我使用的逻辑:
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
}
}))
}))
}))
}))
};
因此endDate
不起作用,也不会继承null
的值,我们将不胜感激任何帮助。
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);
答案 0 :(得分:1)
这里有几个问题要解决。我更改了fullName
中的individualsData
,以使其与updatedGraphTable
的结果匹配(即使根据您的初始值数据不正确),并更改了endDate
在graphData
中,以便我们看到它实际上已更改为null
。
这里的问题是您不能直接期望primaryPosition && primaryPosition.endDate
返回您的null
值,因为它的值被评估为false
,所以它总是回退到item.positions[0].endDate
,您需要类似于带有in
的三元运算符(用于测试属性是否存在,即使该属性为false):
current[item.account.fullName] = (primaryPosition && ('endDate' in primaryPosition)) ? primaryPosition.endDate : item.positions[0].endDate;
然后再一次,因为返回的值是null
并计算为false
,所以accountEndDate[member.account.fullName] || member.position.endDate
将回退到member.position.endDate
,因此您可以在此处使用另一个三元运算符null
。我在这里使用了一个临时变量end
,因此您不需要调用accountEndDate
三次:
endDate: ((end = accountEndDate[member.account.fullName]) || end === null) ? end : member.position.endDate
等等!
const individualsData = [{
"account": {
"id": "001b000003WnPy1AAF",
"fullName": "PonyJeff"
},
"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": "2018-09-09"
},
"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 && ('endDate' in primaryPosition)) ? primaryPosition.endDate : item.positions[0].endDate;
return current;
}, {});
let end;
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: ((end = accountEndDate[member.account.fullName]) || end === null) ? end : member.position.endDate
}
}))
}))
}))
}))
};
console.log(updatedGraphTable);