简体   繁体   中英

JavaScript Array is getting overwritten

i have a Smarty foreach loop where inside it i try to push to a JavaScript array named arrayPorperties . But the content of the arrayPorperties is only getting the last value of all previous values are getting overwritten. Any Solutions?
Note: the assign must be done inside the loop, cause it is getting values from the Smarty array user_properties otherwise it wont work

<script type="text/javascript">user_properties = [];</script>
{​​​​​​​foreach from=$user_properties item="user"}​​​​​​​
    {​​​​​​​assign var='tmp_coords' value=$user->get_coords()}​​​​​​​
        <script>
            user_properties.push(
            ['{$tmp_coords[0]}', '{$tmp_coords[1]}', '{$user->get_name()}', '{$user->get_id()}']
            )
        </script>
{​​​​​​​/foreach}​​​​​​​

I think you can do something like this.

but i dont have test environment. maybe you need to use JSON.Parse after that.

<script type="text/javascript">
    var user_properties = '{ldelim}{$user_properties}{redlim}';
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM