使用数组中的数据更改对象字段

时间:2018-09-12 07:42:26

标签: javascript arrays object

对于我数组中的每个用户,如果'isPrimary'设置为true,我想获取其positionTitle,并使用此positionTitle替换对象中同一用户的所有positionTitle。

两个数据集都有'fullName',我认为应该使用它,因为可能存在多个位置,这使我认为positionID无法使用。

我拥有的代码确实替换了标题,但如果用户有多个职位,则无法使用。

旁注:如果没有主要职位,我希望使用数组中用户的第一个职位。

该对象中的isPrimary本质上是不相关的。

我的对象

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 position我要使用的标题:

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"
          }
        }]
      }]
    }]
  }]
}

我的预期输出。看一下吉姆·宾(Jim Bean)的positionTitles。:

{
  "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"
          }
        }]
      }]
    }]
  }]
}

1 个答案:

答案 0 :(得分:1)

我不完全了解您的数据结构,但如果我认为:

  • IndividualData.account.id不可靠
  • IndividualData.account.fullName是可靠的
  • IndividualData.account.positions是一个数组,每个IndividualData.account包含一个元素

我想出的解决方案是在使用reduce之前过滤具有主要位置的IndividualData.account,并在fullName而不是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中采用以下方法:

  • 如果当前项目具有主要位置,请将其添加到current[item.account.fullName]
  • 否则,如果当前项目的全名没有存储任何内容,请将其添加到current[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;
    }, {} );