gpt4 book ai didi

javascript - 无法将 java List 转换为 javascript ArrayList

转载 作者:太空宇宙 更新时间:2023-11-04 13:15:13 24 4
gpt4 key购买 nike

我正在尝试将Java的字符串列表转换为javascript的ArrayList。代码如下-

var viewByFilterData=JSON.parse(JSON.stringify(('<%=(List<String>)session.getAttribute("filters")%>')));

当我打印 viewByFilterData 变量时,我得到以下输出 -

'[Cameras, Clothing, Computer Hardware, Footwear, Mobile Phones, Watches]'

但是当我尝试使用简单的 for 循环打印上述变量时,我得到了一个又一个的字符('[' 然后 'C' 然后 'a' ...),而不是一次获取一项(例如 '相机' 然后 '服装'...)

关于如何将其转换为 Javascript 数组的任何想法。

最佳答案

这是一个简单的字符串,您可以通过拆分将其转换为数组:

'[Cameras, Clothing, Computer Hardware, Footwear, Mobile Phones,Watches]'
.split(/\[|\]|,/) //to make array
.map(function(c){return c.trim();}) //to get rid of spaces
.filter(function(c){return c.length > 0}) // to get rid of empty elements and '[' and ']'

关于javascript - 无法将 java List 转换为 javascript ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33606698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com