Output enumerables as Javascript data

I have an enumerable data as below

["Mar 2017", "Apr 2017", "May 2017", "Jun 2017", "Jul 2017", "Aug 2017",
 "Sep 2017", "Oct 2017", "Nov 2017", "Dec 2017", "Jan 2018", "Feb 2018"]

intended to be passed to highchart x-axis data.

When i passed the data to using <%= chart_label %> I keep getting

Mar 2017Apr 2017May 2017Jun 2017Jul 2017Aug 2017Sep 2017Oct 2017Nov 2017Dec 2017Jan 2018Feb 2018

Please what is the best way to achieve this?

Thank you.

Have you tried returning

inspect list

instead of your list?

I tried it but was returning

[&quot;Mar 2017&quot;, &quot;Apr 2017&quot;, &quot;May 2017&quot;, &quot;Jun 2017&quot;, &quot;Jul 2017&quot;, &quot;Aug 2017&quot;, &quot;Sep 2017&quot;, &quot;Oct 2017&quot;, &quot;Nov 2017&quot;, &quot;Dec 2017&quot;, &quot;Jan 2018&quot;, &quot;Feb 2018&quot;]

Is there any approach to have the quote instead of "?

If you’re using Phoenix Framework, the phoenix_html library has a nice function called raw/1 that won’t escape it. :slight_smile:

1 Like

I have tried raw initially but the result was below

Mar 2017Apr 2017May 2017Jun 2017Jul 2017Aug 2017Sep 2017Oct 2017Nov 2017Dec 2017Jan 2018Feb 2018

with no quote.

Thanks so much.
I have tried adding raw to the inspect and it gave the required result.

<%= raw inspect(label) %>

Thank you for the pointer.

Please I observed that returning enum with integers converts the the items to a string.

["0", "0", "0", "0", "0", "0", "808500", "0", "0", "0", "0", "0"]

Is there a way to keep it as integer. Highcharts requires data as integer and not string.

inspect is not really a good way to do this, because while it might work here, it will break at least as soon as you try to use dates somewhere in that data. The simple way would be to convert the data to json and output the json in your view.

1 Like

Please can you give a me like a skeleton code to understand how to implement this. I currently compute the chart labels and data in the my view file.

This was an error on my part. It’s showing fine now.

I do all of my Elixir data -> Javascript through JSON serialization, like so:

<script>    
  const label = JSON.parse("<%= label |> Poison.encode!() |> raw() %>")
</script>

It always works!

Thanks so much for the wisdom shared. It worked well.