Javascript 使用数组中的数据更改对象字段
对于数组中的每个用户,如果“isPrimary”设置为true,我希望获取他们的positionTitle,并使用此positionTitle替换对象中同一用户的所有positionTitle 两个数据集都有“全名”,我认为应该使用,因为可能有多个位置,这导致我认为不能使用positionID 我的代码确实替换了标题,但如果用户有多个位置,则代码不起作用 旁注:如果没有主位置,我希望使用阵列中用户的第一个位置 对象中的isPrimary基本上是无关的 我的目标:Javascript 使用数组中的数据更改对象字段,javascript,arrays,object,Javascript,Arrays,Object,对于数组中的每个用户,如果“isPrimary”设置为true,我希望获取他们的positionTitle,并使用此positionTitle替换对象中同一用户的所有positionTitle 两个数据集都有“全名”,我认为应该使用,因为可能有多个位置,这导致我认为不能使用positionID 我的代码确实替换了标题,但如果用户有多个位置,则代码不起作用 旁注:如果没有主位置,我希望使用阵列中用户的第一个位置 对象中的isPrimary基本上是无关的 我的目标: graphData = {
graphData = {
"name": "Annual meetings",
"engagementAreas": [{
"id": "1",
"engagementTypes": [{
"name": "forestry",
"engagements": []
},{
"name": "houses",
"engagements": [{
"name": "engagement1",
"members": [{
"id": "e334", "account": {
"id": "123", "fullName": "jim bean"
},
"position": {
"id": "3434",
"positionTitle": "Manager",
"isPrimary": false
}
}]
}]
},{
"name": "landscaping",
"engagements": [{
"name": "engagement1343",
"members": [{
"id": "e334", "account": {
"id": "123", "fullName": "john boer"
},
"position": {
"id": "4545",
"positionTitle": "Managing Director",
"isPrimary": true
}
},{
"id": "5555", "account": {
"id": "123", "fullName": "jim bean"
},
"position": {
"id": "a16b0000004AxeBAAS",
"positionTitle": "Managing Director",
"isPrimary": true
}
}]
}]
}]
},{
"name": "community days",
"engagementTypes": [{
"name": "skyscraping",
"engagements": []
},{
"name": "tennis",
"engagements": [{
"name": "engagement346",
"members": [{
"id": "34", "account": {
"id": "0010X000048DDMsQAO", "fullName": "edy long"
},
"position": {
"id": "3999434",
"positionTitle": "Managing Director",
"isPrimary": true
}
}]
}]
},{
"name": "Juicing",
"engagements": [{
"name": "347343",
"members": [{
"id": "4546", "account": {
"id": "001b000003WnPy1AAF", "fullName": "jeff bint"
},
"position": {
"id": "35006",
"positionTitle": "Senior Manager, Energy"
}
}]
}]
}]
}]
}
注意Jim bean有两个位置。
我的数组,我要使用它的isPrimary:true位置标题:
IndividualData = [{
"account": {
"id": "23423",
"fullName": "jim bean"
},
"positions": [{
"id": "123",
"organizationId": "001b0000005gxmlAAA",
"organizationName": "a",
"positionTitle": "Dalius Senior Manager, Energy",
"positionLevel": "5-Middle Management & Advisers",
"isPrimary": true,
"startDate": "2016-10-07",
"endDate": null
}]
},{
"account": {
"id": "394838",
"fullName": "jim bean"
},
"positions": [{
"id": "a16b0000004AxeBAAS",
"organizationId": "001b0000005gxmlAAA",
"organizationName": "a",
"positionTitle": "Head Recruiter",
"positionLevel": "Senior Management",
"isPrimary": false,
"startDate": "2008-04-23",
"endDate": null
}]
},{
"account": {
"id": "001b000003WnPy1AAF",
"fullName": "jeff bint"
},
"positions": [{
"id": "a16b0000004AxeBAAS",
"organizationId": "001b0000005gxmlAAA",
"organizationName": "a",
"positionTitle": "Senior Manager, Energy",
"positionLevel": "5-Middle Management & Advisers",
"isPrimary": true,
"startDate": "2016-10-07",
"endDate": null
}]
}, {
"account": {
"id": "0010X000048DDMsQAO",
"fullName": "edy long"
},
"positions": [{
"id": "a160X000004nKfhQAE",
"organizationId": "001b0000005gxmlAAA",
"organizationName": "a",
"positionTitle": "Managing Director",
"positionLevel": "4-Head of Business Unit/Head of Region",
"isPrimary": true,
"startDate": "2018-03-05",
"endDate": null
}]
}, {
"account": {
"id": "123",
"fullName": "john boer"
},
"positions": [{
"id": "325345634634",
"organizationId": "001b0000005gxmlAAA",
"organizationName": "a",
"positionTitle": "Managing Director",
"positionLevel": "4-Head of Business Unit/Head of Region",
"isPrimary": true,
"startDate": "2018-03-05",
"endDate": null
}]
}]
Jim Bean在上面的数组中也有两个位置,但其中一个是主位置
我的代码当前确实替换了,但没有从数组中获取主代码,并更新同一用户的所有positionTitles:
const accountIdToPositionDict = IndividualData.reduce( (current, item) => {
current[item.account.id] = (item.positions.filter( position => position.isPrimary )[0] || {} ).positionTitle;
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: member.account && accountIdToPositionDict[member.account.id] || member.position.positionTitle
}
}))
}))
}))
}))
};
我的当前输出:
{
"name": "Annual meetings",
"engagementAreas": [{
"id": "1",
"engagementTypes": [{
"name": "forestry",
"engagements": []
}, {
"name": "houses",
"engagements": [{
"name": "engagement1",
"members": [{
"id": "e334",
"account": {
"id": "123",
"fullName": "jim bean"
},
"position": {
"id": "3434",
"positionTitle": "Managing Director",
"isPrimary": false
}
}]
}]
}, {
"name": "landscaping",
"engagements": [{
"name": "engagement1343",
"members": [{
"id": "e334",
"account": {
"id": "123",
"fullName": "john boer"
},
"position": {
"id": "4545",
"positionTitle": "Managing Director",
"isPrimary": true
}
}, {
"id": "5555",
"account": {
"id": "123",
"fullName": "jim bean"
},
"position": {
"id": "a16b0000004AxeBAAS",
"positionTitle": "Managing Director",
"isPrimary": true
}
}]
}]
}]
}, {
"name": "community days",
"engagementTypes": [{
"name": "skyscraping",
"engagements": []
}, {
"name": "tennis",
"engagements": [{
"name": "engagement346",
"members": [{
"id": "34",
"account": {
"id": "0010X000048DDMsQAO",
"fullName": "edy long"
},
"position": {
"id": "3999434",
"positionTitle": "Managing Director",
"isPrimary": true
}
}]
}]
}, {
"name": "Juicing",
"engagements": [{
"name": "347343",
"members": [{
"id": "4546",
"account": {
"id": "001b000003WnPy1AAF",
"fullName": "jeff bint"
},
"position": {
"id": "35006",
"positionTitle": "Senior Manager, Energy"
}
}]
}]
}]
}]
}
我的预期产出。看看吉姆·比恩的头衔
{
"name": "Annual meetings",
"engagementAreas": [{
"id": "1",
"engagementTypes": [{
"name": "forestry",
"engagements": []
}, {
"name": "houses",
"engagements": [{
"name": "engagement1",
"members": [{
"id": "e334",
"account": {
"id": "123",
"fullName": "jim bean"
},
"position": {
"id": "3434",
"positionTitle": "Dalius Senior Manager, Energy",
"isPrimary": false
}
}]
}]
}, {
"name": "landscaping",
"engagements": [{
"name": "engagement1343",
"members": [{
"id": "e334",
"account": {
"id": "123",
"fullName": "john boer"
},
"position": {
"id": "4545",
"positionTitle": "Managing Director",
"isPrimary": true
}
}, {
"id": "5555",
"account": {
"id": "123",
"fullName": "jim bean"
},
"position": {
"id": "a16b0000004AxeBAAS",
"positionTitle": "Dalius Senior Manager, Energy",
"isPrimary": true
}
}]
}]
}]
}, {
"name": "community days",
"engagementTypes": [{
"name": "skyscraping",
"engagements": []
}, {
"name": "tennis",
"engagements": [{
"name": "engagement346",
"members": [{
"id": "34",
"account": {
"id": "0010X000048DDMsQAO",
"fullName": "edy long"
},
"position": {
"id": "3999434",
"positionTitle": "Managing Director",
"isPrimary": true
}
}]
}]
}, {
"name": "Juicing",
"engagements": [{
"name": "347343",
"members": [{
"id": "4546",
"account": {
"id": "001b000003WnPy1AAF",
"fullName": "jeff bint"
},
"position": {
"id": "35006",
"positionTitle": "Senior Manager, Energy"
}
}]
}]
}]
}]
}
我不完全理解您的数据结构,但如果我假设:
不可靠个人数据.account.id
可靠IndividualData.account.fullName
是一个数组,每个IndividualData.account.positions
IndividualData.account
个人数据。帐户
s在使用reduce之前有一个主要位置,并在全名
而不是Id
上执行整个操作:
const accountIdToPositionDict = IndividualData
.filter(item => item.positions.find(p => p.isPrimary))
.reduce( (current, item) => {
current[item.account.fullName] = (item.positions.find( position => position.isPrimary ) || {} ).positionTitle;
return current;
}, {} );
const updatedGraphTable = {
//Long stuff to get to the relevant path...
accountIdToPositionDict[member.account.fullName] || member.position.positionTitle
}
编辑
根据您的评论,如果用户在IndividualData中没有主位置,则必须将其位置设置为该用户在IndividualData中获得的第一个位置。在这种情况下,您可以删除我前面代码片段的过滤器部分,在reduce中使用以下方法:
- 如果当前项目具有主位置,则将其添加到当前[item.account.fullName]键中
- 否则,如果没有为当前项目的全名存储任何内容,请将其添加到
项中当前[item.account.fullName]
const accountIdToPositionDict = IndividualData .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; }, {} );