使用数组中的数据更改对象字段 [英] Change Object Fields with Data from Array

查看:71
本文介绍了使用数组中的数据更改对象字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For each user in my array I want to take their positionTitle if the 'isPrimary' is set to true and use this positionTitle to replace all positionTitle's of the same user in my object.

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

Both data sets have 'fullName' which I think should be used as there can be multiple positions which leads me to think the positionID can not be used.

我所拥有的代码确实会替换标题,但如果用户有多个职位则不起作用。

The code I have does replace the titles but doesn't work if a user has multiple positions.

附注:如果没有主要位置我想要使用数组中的用户的第一个位置。

Side note: if there are no primary positions I would like the first position for a user in the array to be used.

对象中的isPrimary基本上是无关紧要的。

The isPrimary in the object is basically irrelevant.

我的对象:

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:我想要使用的真实位置:

Notice Jim bean has two position. My array, whose isPrimary: true positionTItles I want to use:

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还有两个上面数组中的位置,但是一个是主要的。

Jim Bean also has two positions in the array above, but one is primary.

我的代码当前确实替换但是没有从数组中获取主数据并更新同一用户的所有positionTitles: / p>

my code which currently does replace but doesnt take the primary from the array and update all positionTitles for the same user:

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的位置标题。:

My Expected output. look at Jim Bean's 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"
          }
        }]
      }]
    }]
  }]
}


推荐答案

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

I don't fully understand your data structure, but if I assume that :


  • IndividualData.account.id 不可靠

  • IndividualData.account.fullName 是reliab le

  • IndividualData.account.positions 是一个数组,每个 IndividualData.account

  • IndividualData.account.id is not reliable
  • IndividualData.account.fullName is reliable
  • IndividualData.account.positions is an array that contains one element per IndividualData.account

我想出的解决方案是过滤 IndividualData.account 在使用reduce之前具有主要位置的s,并在 fullName 而不是 Id 上执行整个操作:

The solution I came up with is to filter the IndividualData.accounts that has a primary position before using your reduce, and do the whole thing on fullName instead of 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中为该用户获得的第一个职位。在这种情况下,您可以删除我之前代码段的过滤器部分,并在您的缩减中使用以下方法:

Edit

According to your comment, if a user has no primary position in IndividualData, you have to set his position to the first position you get for this user in IndividualData. In that case, you can drop the filter part of my previous snippet and go for the following approach in your reduce:


  • 如果当前项目有一个主要位置,将其添加到当前[item.account.fullName]

  • 否则,如果有没有为当前项目的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;
}, {} );


这篇关于使用数组中的数据更改对象字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆