如何在javascript中将对象转换为列表?

时间:2018-12-29 05:51:48

标签: javascript node.js ejs

我需要将此对象转换为expressJS视图中的列表(使用EJS):

{https://ae01.alicdn.com/kf/HTB12Xa4ipGWBuNjy0Fbq6z4sXXa3/Mickey-Mouse-Microwave-Glove-Potholder-Bakeware-Blue-and-White-100-Cotton-Oven-Mitts-and-Potholder-mat.jpg,https://ae01.alicdn.com/kf/HTB10xfciv9TBuNjy1zbq6xpepXaG/Mickey-Mouse-Microwave-Glove-Potholder-Bakeware-Blue-and-White-100-Cotton-Oven-Mitts-and-Potholder-mat.jpg,https://ae01.alicdn.com/kf/HTB13AHqcfiSBuNkSnhJq6zDcpXav/Mickey-Mouse-Microwave-Glove-Potholder-Bakeware-Blue-and-White-100-Cotton-Oven-Mitts-and-Potholder-mat.jpg}

我尝试过:

<% const xx = result.data[0].galleryimages.replace('{',"['").replace('}',"']").split(',') %>



        <% for (let j=0; j<xx.length; j++) { %>
           <%= xx[j]%>

        <% } %>

但它返回:

['https://ae01.alicdn.com/kf/HTB12Xa4ipGWBuNjy0Fbq6z4sXXa3/Mickey-Mouse-Microwave-Glove-Potholder-Bakeware-Blue-and-White-100-Cotton-Oven-Mitts-and-Potholder-mat.jpg https://ae01.alicdn.com/kf/HTB10xfciv9TBuNjy1zbq6xpepXaG/Mickey-Mouse-Microwave-Glove-Potholder-Bakeware-Blue-and-White-100-Cotton-Oven-Mitts-and-Potholder-mat.jpg https://ae01.alicdn.com/kf/HTB13AHqcfiSBuNkSnhJq6zDcpXav/Mickey-Mouse-Microwave-Glove-Potholder-Bakeware-Blue-and-White-100-Cotton-Oven-Mitts-and-Potholder-mat.jpg']

2 个答案:

答案 0 :(得分:0)

您发布的内容不是对象,而是字符串-试试:

const xx = result.data[0].galleryimages.replace(/[{}]/, '').split(',')

答案 1 :(得分:0)

问题解决了

<% let xx = result.data[0].galleryimages.slice(1,-1).split(",") %> 
        <% for (let j=0; j<xx.length; j++) { %>
            <% let ha = xx[j].slice(1,-1); %>
           <img src=<%= ha.replace("'","") %> width="10%"/>
        <% } %>