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
3 Answers
Reset to default 6Replace
$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;