最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - concatenating strings in .each loop - Stack Overflow

programmeradmin2浏览0评论

This may be a dumb question but I can't seem to get it or find it anywhere.

I have a .each function that returns the id's of a bunch of divs on a page and then assigns them a number. I need them to be outputted in a specific format all as one string so I can pass them to a database and use them as "sort_order" values. (I split them through a sproc in my database).

Here is my code:

jQuery('#updateAllContentButton').click(function() {
    var count = 1;
 jQuery('.CommentaryItem').each(function() {
    var id = GetID(jQuery(this));
        var data_str = (id + ':' + count + '~');
        console.log(data_str);
        count++;
    });
});

So that returns each data_str on a separate line but I need it to return it like this: 144:2~145:3~146:4~147:4~148:5 (so on)

Any help would be appreciated, thanks!

This may be a dumb question but I can't seem to get it or find it anywhere.

I have a .each function that returns the id's of a bunch of divs on a page and then assigns them a number. I need them to be outputted in a specific format all as one string so I can pass them to a database and use them as "sort_order" values. (I split them through a sproc in my database).

Here is my code:

jQuery('#updateAllContentButton').click(function() {
    var count = 1;
 jQuery('.CommentaryItem').each(function() {
    var id = GetID(jQuery(this));
        var data_str = (id + ':' + count + '~');
        console.log(data_str);
        count++;
    });
});

So that returns each data_str on a separate line but I need it to return it like this: 144:2~145:3~146:4~147:4~148:5 (so on)

Any help would be appreciated, thanks!

Share Improve this question asked Jul 9, 2013 at 13:45 J215DJ215D 931 gold badge2 silver badges8 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 11

Use map and join :

jQuery('#updateAllContentButton').click(function() {
    console.log(jQuery('.CommentaryItem').map(function(i) {
        return GetID(jQuery(this)) + ':' + (i+1);
    }).get().join('~'));
});

Note that you don't have to count yourself : Most jQuery iteration functions do it for you.

You can use an array for that. Like this...

jQuery('#updateAllContentButton').click(function() {
    var count = 1;
    var arr = [];
 jQuery('.CommentaryItem').each(function() {
    var id = GetID(jQuery(this));
    var data_str = (id + ':' + count + '~');
    arr.push(data_str);
    //console.log(data_str);
    count++;
 });
 console.log(arr.join());
});

try this:

jQuery('#updateAllContentButton').click(function() {
var count = 1;
var data_str = "";
jQuery('.CommentaryItem').each(function() {
var id = GetID(jQuery(this));
    data_str += (id + ':' + count + '~');
    console.log(data_str);
    count++;
});

});

change

var data_str = (id + ':' + count + '~');

to

data_str+ = (id + ':' + count + '~');

full code

jQuery('#updateAllContentButton').click(function() {
    var count = 1,
        data_str;

    jQuery('.CommentaryItem').each(function() {
        var id = GetID(jQuery(this));
        data_str += (id + ':' + count + '~');
        console.log(data_str);
        count++;
    });
});
发布评论

评论列表(0)

  1. 暂无评论