1

just wanting to set up an array through my parameters. Check out this pseudocode bellow to see what I'm trying to do:

function someFunction(x = new Array()){
 for (a in x){
  some action with x[a];
 }
}

someFunction([value a, value b, value c]);

So I'm trying to do something that may work similar to this. Thanks in advance guys!

1
  • Don't use new Array; just doing [] works the same and is way simpler. Commented Jul 18, 2011 at 18:13

3 Answers 3

2

If I understand you right you want to use empty array in case no parameters were passed to function. You can the following code to achieve this.

function someFunction(x){
    x = x || [];
    //...    
}

someFunction([value a, value b, value c]);
Sign up to request clarification or add additional context in comments.

4 Comments

Oh wow, I was pretty close wasn't I? so the x = x || []; declares the parameter , in this case x, to be an array then?
x = x || [] says to assign the value of x to x if x has a value (is not undefined or false or null). If x has no value, then make it an empty array. It's a very useful way to deal with optional parameters.
Hold up, without filtering for hasOwnProperty, you're going to get all of the member functions o the array along with the actual array values. Here's a jsfiddle that demonstrates. Whenever you're doing a for-in loop, you should filter hasOwnProperty as a matter of course.
Actually it is better to avoid using for ... in ... for iteration over arrays. I only wanted to demonstrate the technic of working with params. Edited the snippet.
0

That's basically a special case of each:

function each(a, f) {
    a = a || [];
    for (var i=0; i < a.length; ++i) {
        f(a[i]);
    }
}

though each is usually implemented as a method of Array, along with map, filter and reduce/fold:

Array.prototype.each = function(f) {
    for (var i=0; i < this.length; ++i) {
        f(this[i], i);
    }
};

Array.prototype.map = function(f) {
    var result = [];
    for (var i=0; i < this.length; ++i) {
        result[i] = f(this[i], i);
    }
    return result;
};

Array.prototype.map = function(keep) {
    var result = [];
    for (var i=0; i < this.length; ++i) {
        if (keep(this[i], i)) {
            result.push(this[i]);
        }
    }
    return result;
};

Array.prototype.foldl = function(f) {
    var result = this[0];
    for (var i=1; i < this.length; ++i) {
        result = f(result, this[i]);
    }
    return result;
};

Array.prototype.foldr = function(f) {
    var i=this.length-1
    var result = this[i];
    for (--i; i >= 0; --i) {
        result = f(this[i], result);
    }
    return result;
};

Array.prototype.reduce = Array.prototype.foldl;

Note that using for (... in ...) with arrays can cause problems as it will pick up properties defined on an array in addition to the integer indices.

1 Comment

Be aware that there are pitfalls to altering the global Array prototype.
0

Try this:

function someFunction() {
    for(var i = 0; i < arguments.length; i+=1) {
       // some action with arguments[i]
    }
}

// usage
someFunction(a, b, c);
someFunction.apply(window, [a, b, c]);

The arguments property is automatically defined inside a function, with a new one for each function scope. It's almost an array, but not quite. You can use it like an array in 99% of cases, though.

The second usage example is if you have an array that you want to pass in, but it's not defined statically. The apply method on the function object will let you pass in a context and an array of arguments.

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.