briosheje
briosheje

Reputation: 7446

Javascript array.prototype being printed almost everywhere

Since I had to remove some elements from my arrays, I followed a few pieces of code found in stackoverflow and came up with this one:

Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

For some reasons, however, this piece of code is being printed EVERYWHERE whenever something has something to with an array.

Example:

this piece of code:

Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

Scadenza.prototype.init = function() {
    this.promemoria = (this.promemoria == "")?("NO"):(this.promemoria);
    var gruppo = this.group; // convert array to string.
    this.group = "";
    for (var i in gruppo) {
        if (i != (gruppo.length - 1)) {
            this.group += gruppo[i] + ", ";
        }
        else {
            this.group += gruppo[i];
        }
    }
    alert(this.group);
};

This piece of code is supposed to convert the array this.group (stored temporarily in the variable "gruppo") into a string (that's quite obvious, I think).

It is, of course, doing it's job greatly, if it wouldn't be that its alert is:

[DATA NEEDED]function (from, to) { var rest = this.slice((to || from) + 1 || this.length); this.length = from < 0 ? this.length + from : from; return this.push.apply(this, rest); }

This piece of code is being also sent to a database through an AJAX request and the result of the query, at the desired coloumn, is this one:

function (from, to) { var rest = this.slice((to || from) + 1 || this.length); this.length = from < 0 ? this.length + from : from; return this.push.apply(this, rest); },

I'm quite surprised this is happening but I have ABSOLUTELY no idea on how to fix it.

No errors have been alerted while loading the page or when clicking the button that throws this event.

Any idea?

ps: Not sure if helps, but I'm using jQuery.

@comments:

The normal for loop actually doesn't fix this:

Scadenza.prototype.init = function() {
    this.promemoria = (this.promemoria == "")?("NO"):(this.promemoria);
    var gruppo = this.group; // convert array to string.
    this.group = "";
    for (var i = 0; i < gruppo.length; i++) {
        if (i != (gruppo.length - 1)) {
            this.group += gruppo[i] + ", ";
        }
        else {
            this.group += gruppo[i];
        }
    }
    alert(this.group);
};

Alert is still the same.

Upvotes: 0

Views: 421

Answers (2)

Eric H.
Eric H.

Reputation: 7014

When you change the Array prototype, the method is added to the Array object as a new attribute. When you iterate with for (var attr in object) you are iterating over the object and all of its attributes. Therefore, your new method is included in that loop.

You need to use a for (var i=0; i<a.length; i++) loop. That will only include items in the array.

Upvotes: 3

Bergi
Bergi

Reputation: 664548

Use proper for (var i=0; i<arr.length; i++) loops for iterating arrays. for in enumerations will enumerate prototype properties as well, do not use them on arrays. You are doing that in your init method for example.

Btw, for that task you want to use .join anyway:

Scadenza.prototype.init = function() {
    if (this.promemoria == "")
        this.promemoria = "NO";
    this.group = this.group.join(", "); // convert array to string
    alert(this.group);
};

Upvotes: 4

Related Questions