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

javascript - How to solve "has no method 'push'" error? - Stack Overflow

programmeradmin0浏览0评论

using the following code

$.getJSON('services/getCharts.php', function(json) {
    var $line1 = [];
    $.each(json.posts, function() {
        $line1 = $line1.push([this.post.rectime,this.post.actual_value]);
    });
...
});

with JQuery gives me the following error:

Uncaught TypeError: Object 1 has no method 'push'

Could anyone help me finding what is wrong? Thanks a lot

using the following code

$.getJSON('services/getCharts.php', function(json) {
    var $line1 = [];
    $.each(json.posts, function() {
        $line1 = $line1.push([this.post.rectime,this.post.actual_value]);
    });
...
});

with JQuery gives me the following error:

Uncaught TypeError: Object 1 has no method 'push'

Could anyone help me finding what is wrong? Thanks a lot

Share Improve this question asked Apr 3, 2013 at 16:07 vpxvpx 3821 gold badge5 silver badges14 bronze badges 1
  • MDN .push() method "Mutates an array by appending the given elements and returning the new length of the array." – user1106925 Commented Apr 3, 2013 at 16:20
Add a ment  | 

3 Answers 3

Reset to default 6

Replace

    $line1 = $line1.push([this.post.rectime,this.post.actual_value]);

with

    $line1.push([this.post.rectime,this.post.actual_value]);

push changes the receiver array and returns the new length.

You get this precise error message because the length (1) is promoted as a Number when you try to call the push method on it.

The push method returns the new length of the array, not a new array and it modifies the array it is being called upon:

$.each(json.posts, function() {
    $line1.push([this.post.rectime,this.post.actual_value]);
});

here's a really tight syntax:

  return Array.reduce(things, function(total, thing) {
    return total.concat([thing]);
  }, [])

This:

return total.concat([result])

Is similar in effects to this:

total.push(result);
return total;
发布评论

评论列表(0)

  1. 暂无评论