Accessing Perl Array in JavaScript

自闭症网瘾萝莉.ら 提交于 2019-12-02 10:58:55
ikegami

To check for existence, you'd want a hash. A simple way of transmitting the data would be to encode it using JSON.

% use JSON qw( );

<script type="text/javascript">

var testArray = <% JSON->new()->encode({ map { $_ => 1 } get_values() }) %>;

function checkIfValExistsInTestArray(val) {
   return testArray[val];
}

</script>

For example, if get_values() returned apple and orange, you'd get

<script type="text/javascript">

var testArray = {"apple":1,"orange":1};

function checkIfValExistsInTestArray(val) {
   return testArray[val];
}

</script>

I don't know Mason, so there could be errors, but you get the idea.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!