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

javascript - jQuery.each return indexed array - Stack Overflow

programmeradmin5浏览0评论

I have this code:

function gatherFollowers() {
            var followers = [];
            jQuery('.follower').each(function(index, value) {
                var i = parseInt(jQuery(this).attr('data-id'));
                followers.push(i);
            });
            return followers;
        }

That should grab all attributes but in result it returns this:

[100, 99, 88, 72, 63, 34, 31, 30, 29, each: function, eachSlice: function, all: function, any: function, collect: function…]

How could I make it return simple array like this:

[4,29,30,31,32,34,36,37,60,61,63,72,76,77,88,99,100] 

Thanks

I have this code:

function gatherFollowers() {
            var followers = [];
            jQuery('.follower').each(function(index, value) {
                var i = parseInt(jQuery(this).attr('data-id'));
                followers.push(i);
            });
            return followers;
        }

That should grab all attributes but in result it returns this:

[100, 99, 88, 72, 63, 34, 31, 30, 29, each: function, eachSlice: function, all: function, any: function, collect: function…]

How could I make it return simple array like this:

[4,29,30,31,32,34,36,37,60,61,63,72,76,77,88,99,100] 

Thanks

Share Improve this question asked Mar 11, 2013 at 19:46 ShiftedRealityShiftedReality 3124 silver badges15 bronze badges 8
  • 1 Show us your code that prints the result of this function – Benjamin Gruenbaum Commented Mar 11, 2013 at 19:47
  • Can you also post the code showing what has the class "follower" – tymeJV Commented Mar 11, 2013 at 19:48
  • there is foreach with <div class="follower" data-id="{$id}"></div> – ShiftedReality Commented Mar 11, 2013 at 19:57
  • I don't believe that's the result you get from this code. Seems to me that you have prototypejs loaded in addition to jQuery, and you're improperly enumerating your resulting array using for-in, which includes the prototyped functions. If the functions were actually directly in the Array as you show, their result would be NaN. – the system Commented Mar 11, 2013 at 20:01
  • 1 @ShiftedReality: Yes, I know. The issue has nothing to do with the answers below. The issue is that you're using for-in to enumerate your resulting Array. Am I correct that you're using for-in on the result of the gatherFollowers() function? – the system Commented Mar 11, 2013 at 20:15
 |  Show 3 more ments

4 Answers 4

Reset to default 5

I'm sure you can use map() to get what you need - and .get()

function gatherFollowers() {
    return jQuery('.follower').map(function(i,v){
        return $(v).data('id');
    }).get();
}

FIDDLE

Try with

function gatherFollowers() {
    var followers = $.map($('.follower'), function(value) {
        return parseInt($(this).data('id'), 10);
    });

    // followers should be an array of numbers
}

I've changed your code to use $.map and also to use .data() to access the HTML 5 data- id attribute.

Well, in fact pushing this function result into object really helped me followers.data = gatherFollowers();

Now console.log shows

Object {data: Array[9], type: "custom"}
data: Array[9]
0: "100"
1: "99"
2: "88"
3: "72"
4: "63"
5: "34"
6: "31"
7: "30"
8: "29"
length: 9
__proto__: Array[0]
type: "custom"

as expected.

I tried this:

//HTML
//<div id="1" class="follower">follower1</div>
//<div id="2" class="follower">follower2</div>

var followers = [];
$('.follower').each( function(index, value){
    var follower_id = $('.follower:eq(' + index + ')').attr('id');
    followers.push(follower_id)
});
console.log(followers)
//output [1,2]
发布评论

评论列表(0)

  1. 暂无评论