@Sparlarva обновил мой ответ

аждого пользователя в моем массиве я хочу взять их positionTitle, если для isPrimary установлено значение true, и использовать 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"

          }
        }]
      }]
    }]
  }]
}

Обратите внимание, что у Джима Бина две позиции. Мой массив, для которого isPrimary: true positionTItles я хочу использовать:

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

Джим Бин также имеет две позиции в массиве выше, но одна является основной.

мой код, который в настоящее время заменяет, но не берет основной из массива и обновляет все 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"
          }
        }]
      }]
    }]
  }]
}

Ответы на вопрос(1)

Ваш ответ на вопрос