1

I am using a an array called "workingLinks", in my javascript function first I define the array as

workingLinks = [];

Then I use console

console.log(workingLinks); 

Now the console displays as

[remove: function] ;

here if i use a for loop it is iterating over the function which i don't want. If I create an array in console then it works fine, shows [] as expected. Anybody have the idea of how the array holds remove function?

8
  • 6
    Looks like you or your frameworrk added a remove method to the Array prototype. Commented Oct 4, 2014 at 20:44
  • yes I am using d3 framework with other few libraries, how to remove this temporarily? Commented Oct 4, 2014 at 20:47
  • D3 itself doesn't appear to be the culprit: jsfiddle.net/aqa60c43/2. Check your other libraries if they mess with the Array prototype. Commented Oct 4, 2014 at 20:49
  • can I remove the function from prototype? Commented Oct 4, 2014 at 20:50
  • I wouldn't recommend this, since the remove function is probably being used elsewhere, but try: delete Array.prototype.remove Commented Oct 4, 2014 at 20:51

2 Answers 2

2

I don't know about the remove function, the guys in the comments were pretty helpful.

However, you can simply iterate an array with the forEach function like so:

arr.forEach(functionToRunForEachElement);

It also accepts anonymous functions:

arr.forEach(function(element, index, originalArray) {
   //Do stuff here
});

There are also .map and .reduce to perform common actions like mapping all the values of the array to something else, or reduce the array of values into a single value.

Sign up to request clarification or add additional context in comments.

1 Comment

Note that forEach will only work in browsers that support ES5, but you can find shims for older browsers, or use a libraries like underscore and lodash, which have an each utility method that works on arrays.
1

Something has modified the Array prototype. I tried this in Chrome:

Array.prototype.remove = function () {};
> function () {}
console.log([]);
> [remove: function]

What other libraries are you using?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.