如何为for循环中的每个项添加分隔符,期望最后一个 [英] How to add separator to each item in for loop expect last one

查看:80
本文介绍了如何为for循环中的每个项添加分隔符,期望最后一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的循环中,如何从循环中的latt键中删除逗号?

In the loop below, how can I remove the comma from the latt key in the loop?

var result = 'These are the results: ';
jQuery.each(item['keyterms']['terms'],function(i,kw){
for (key in keywords){
sep = ',';
    if (keywords[key] > 5) result += '<span>' + key + sep + '</span>';
}}


推荐答案

您可以使用标准的javascript函数加入

Instead of inserting coma inside loop you can use standard javascript function JOIN

var results = Array();

for (key in keywords){
    if (keywords[key] > 5) results.push('<span>' + key + '</span>');
}

var message = 'These are the results: ' + results.join(',');

这篇关于如何为for循环中的每个项添加分隔符,期望最后一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆