How to get a value of an object when the key of the value is variable

My problem :

I need to modify existing data to access a value in an object using a variable key but the key remains undefined

My code:
position is a radio input. The position value is stored using a react hook form

in my main component, I call ‘position’ with watch to reuse it.

I then have a reusable form which must use this ‘position’ parameters

in this form, I have a table which must use data from an API

the data received (articles) is an array of objects
the object looks like this

{
"id": "b56",
"app_id": "SC-STD-10",
"name": "test - Standard - 10 hours",
"service_hours": 10,
"price": {
"first_sell_price": 2280,
"second_sell_price": 2260,
"third_sell_price": 2220
}
},

My generator page, where i get articles.

//here all my import


export const Generator = () => {
  const dispatch = useDispatch();
  const methods = useForm();

  const step = useSelector((state) => state.step.actual);
  const positions = useSelector((state) => state.positions?.positions);
  const articles = useSelector((state) => state.articles?.articles);

  const category = methods.watch('category');
  const service = methods.watch('service');
  **const position = methods.watch('position');**
  const customer = methods.watch('customer');

  const mergeFormConfigurations = (baseConfig, additionalConfig) => {
    return { ...baseConfig, ...additionalConfig };
  };

  const getFormConfiguration = () => {
    let combinedConfigurations = formConfiguration(
      category,
      positions
    );
    if (service?.name === 'Refill support contract') {
      combinedConfigurations = mergeFormConfigurations(
        combinedConfigurations,
        **refillSupportContractFormConfiguration(articles, position)**
      );
    }
    return combinedConfigurations;
  };

  const currentFormConfiguration = **getFormConfiguration**();

  const FormWrapper = ({ step, methods }) => {
    const configuration = currentFormConfiguration[step];
    if (!configuration) {
      return null;
    }

    const onSubmit = (data) => {
      postOffer(data);
    };

    return (
      <Box
        sx={{
          width: '80vw',
          margin: '0 5em',
        }}
      >
        <form onSubmit={methods.handleSubmit(onSubmit)}>
          <NewOffer />
          <Save />

          <FormComponent
            title={configuration.title}
            InputFields={configuration.InputFields}
          />
        </form>
      </Box>
    );
  };

  useEffect(() => {
    dispatch(setLocation('generator'));
     dispatch(getPositions());
  }, []);



  return (
    <Box>
      <FormProvider {...methods}>
        <StepsBar />
        <FormWrapper step={step} methods={methods} />
      </FormProvider>
    </Box>
  );
};

in order to use its data in my form, I must modify the data received to add ‘category’ to them and add a price according to the selected position

here, the position log gives me the selected position (THIRD)

However, when I try to access finalPrice with the priceKey which uses position, I get undefined

On the other hand, if I decide to access it by passing the key directly (article.price[‘third_sell_price’])

it works.

Why?

Is this a problem because of the asynchronous?

export const refillSupportContractFormConfiguration = (articles, position) => {
  console.log(position, 'position');
  const updatedArticles = articles.map((article) => {
    const priceKey = Object.keys(article?.price).find((key) => {
      console.log(position?.toLowerCase());
      return key.includes(position?.toLowerCase());
    });

    const finalPrice = article.price[priceKey];
    console.log(finalPrice);
    return {
      ...article,
      quantity: 0,
      finalPrice: finalPrice,
    };
  });
  return {
    'Test': {
      title: 'Test title',
      InputFields: [
        {
          inputType: 'array',
          inputTitle: 'Change the quantity of articles you want',
          rows: updatedArticles,
          columns: [
            {
              field: 'name',
              headerName: 'Package',
              flex: 1,
            },
            {
              field: 'quantity',
              headerName: 'Quantité',
              type: 'number',
              editable: true,
              flex: 1,
            },
            {
              field: 'finalPrice',
              headerName: 'Final price',
              type: 'number',
              width: 120,
              valueGetter: (params) =>
                params.row.quantity * params.row.finalPrice,
              flex: 1,
            },
          ],
          register: 'test',
        },
      ],
    },
  };
};

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật