下面是我的尝试,我有一个对象,其中包含对象数组,其中包含一个字段:“ positionTitle”。
我还有一个对象数组,其中也有一个'positionTitle'
它们都具有相似的数据,我希望我的“ individualsData”中所有positionTitles的值都进入“ graphData”,并且现在能够使用此新的graphData!
我认为我的尝试是错误的,将它们都视为数组?
谢谢,戴尔
graphData = {
"engagementAreas": [{
"id": "1",
"engagementTypes": [{
"name": "forestry",
"engagements": []
},
{
"name": "houses",
"engagements": [{
"name": "engagement1",
"members": [{
"position": {
"id": "3434",
"positionTitle": "Manager"
}
}]
}]
}
]
}]
}, {
"name": "landscaping",
"engagements": [{
"name": "engagement1343",
"members": [{
"position": {
"id": "4545",
"positionTitle": "Senior Manager"
}
}]
}]
}
IndividualData = [{
"account": {
"id": "001b000003WnPy1AAF",
"fullName": "Adnan A. Khan"
},
"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": "Christine Leong"
},
"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
}]
}
What I expect to see:
NEWgraphData = {
"engagementAreas": [{
"id": "1",
"engagementTypes": [{
"name": "forestry",
"engagements": []
},
{
"name": "houses",
"engagements": [{
"name": "engagement1",
"members": [{
"position": {
"id": "3434",
"positionTitle": "Senior Manager, Energy" <== from individualsdata
}
}]
}]
}
]
}]
}, {
"name": "landscaping",
"engagements": [{
"name": "engagement1343",
"members": [{
"position": {
"id": "4545",
"positionTitle": "Managing Director" <== also from individuals data
}
}]
}]
}
graphData.engagementAreas.map((el, i) => {
el.engagementTypes.engagements.members.position.positionTitle = individualsData.positions.positionTitle;
return el;
})
答案 0 :(得分:0)
const graphData = {
"engagementAreas": [{
"id": "1",
"engagementTypes": [{
"name": "forestry",
"engagements": []
},
{
"name": "houses",
"engagements": [{
"name": "engagement1",
"members": [{
"position": {
"id": "a16b0000004AxeBAAS",
"positionTitle": "Manager"
}
}]
}]
}
]
}]
};
const IndividualData = [{
"account": {
"id": "001b000003WnPy1AAF",
"fullName": "Adnan A. Khan"
},
"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": "Christine Leong"
},
"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": "001b000000Gs91HAAR",
"fullName": "Cora Ansorg"
},
"positions": [{
"id": "a16b00000022KmhAAE",
"organizationId": "001b0000006291oAAA",
"organizationName": "a",
"positionTitle": "Strategy Consultant - Financial Services",
"positionLevel": "5-Middle Management & Advisers",
"isPrimary": false,
"startDate": "2012-01-01",
"endDate": "2015-04-30"
}]
}]
const allPositions = _.flatten(IndividualData.map(i => i.positions))
const updateMembers = members => members.map(member => {
const position = allPositions.find(pos => pos.id === member.position.id) || {
positionTitle: 'untitiled'
};
return { ...member,
position: { ...member.position,
positionTitle: position.positionTitle
}
};
});
const updateEngagements = engagements => (
engagements.map(engagement => ({
...engagement,
members: updateMembers(engagement.members)
}))
);
const updateEngagementTypes = (engagementAreas) => ({
...engagementAreas,
engagementTypes: engagementAreas
.engagementTypes.map(type => ({
...type,
engagements: updateEngagements(type.engagements)
}))
});
const updatedEngagementAreas = graphData.engagementAreas.map(updateEngagementTypes);
console.log('UpdatedEngagementAreas mapped based on position id', updatedEngagementAreas)
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>