Drupal 8: how to generate options based on Ajax callback
The below code is working well forme.

public function myAjaxCallback(array &amp;$form, FormStateInterface $form_state) {
  if ($selectedValue = $form_state-&gt;getValue('example_select')) {
    $arr = array('1' =&gt; 'Nice way', '2' =&gt; 'Good way');
    $form['example_select2']['#options'] = $arr;
  }
  $form_state-&gt;setRebuild(TRUE);
  $response = new AjaxResponse();
  $response-&gt;addCommand(new ReplaceCommand("#first", ($form['example_select2'])));
  return $response;
}