Twig, since it's written in PHP
, runs on the server, completely separately than the javascript code, so what you want needs a workaround.
First, generate the route, but with a placeholder, then replace that with the value of your variable when neccessary:
var route = "{{ path('post_display', { 'id': "PLACEHOLDER" }) }}";
window.location = route.replace("PLACEHOLDER", js_variable);
Something like this should work for you.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…