Site icon Hip-Hop Website Design and Development

How to add ajax url to js using wp_add_inline_script()?

I usually enable ajax in a js script by using wp_localize_script like this

wp_localize_script( 'map-scripts', 'ajax_info', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );

but now I see it is best practice to use wp_add_inline_script, which I find harder to use.

I am not able to pass an array with this method. I tried the following but it does not look the same:

$ajax_arr = array( 'ajax_url' => admin_url( 'admin-ajax.php' ) );
$ajax_json = json_encode($ajax_arr);
$ajax_info  = 'ajax_info = '.json_encode($ajax_json).';';
wp_add_inline_script( 'map-scripts', $ajax_info, 'before' );

Do you know the proper way to do it?