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

JavaScript - for Loop vs. Array shift - Stack Overflow

programmeradmin2浏览0评论

I have two functions, they do look alike but what I don't really understand is when inside the for-loop, since the input is an array, why doesn't the array need any index to call the first array?

I have an array of...

var puzzlers = [
function(a) { return 8 * a - 10; },
function(a) { return (a - 3) * (a - 3) * (a - 3); },
function(a) { return a * a + 4; },
function(a) { return a % 5; }
];

I'm trying to loop through the array with an input. The result of the first function will then be used as the next function's input then the first array will be removed.

This is what I wrote...

function applyAndEmpty(input, queue)
{
    var length = queue.length;
    for(var i = 0; i < length; i++)
    {
        input = queue[0](input);
        queue.shift();
    }
    return input;
}

The above does give me the answer but then I see that there's another way of writing it which is

var applyAndEmpty = function(input, queue)
{
    var length = queue.length;
    for(var i = 0; i < length; i++)
    {
        input = queue.shift()(input);
    }
    return input;
};

What I don't understand is the part input = queue.shift()(input).

Doesn't the queue need an index?

I have two functions, they do look alike but what I don't really understand is when inside the for-loop, since the input is an array, why doesn't the array need any index to call the first array?

I have an array of...

var puzzlers = [
function(a) { return 8 * a - 10; },
function(a) { return (a - 3) * (a - 3) * (a - 3); },
function(a) { return a * a + 4; },
function(a) { return a % 5; }
];

I'm trying to loop through the array with an input. The result of the first function will then be used as the next function's input then the first array will be removed.

This is what I wrote...

function applyAndEmpty(input, queue)
{
    var length = queue.length;
    for(var i = 0; i < length; i++)
    {
        input = queue[0](input);
        queue.shift();
    }
    return input;
}

The above does give me the answer but then I see that there's another way of writing it which is

var applyAndEmpty = function(input, queue)
{
    var length = queue.length;
    for(var i = 0; i < length; i++)
    {
        input = queue.shift()(input);
    }
    return input;
};

What I don't understand is the part input = queue.shift()(input).

Doesn't the queue need an index?

Share Improve this question edited Oct 12, 2016 at 23:04 Roko C. Buljan 206k41 gold badges327 silver badges335 bronze badges asked Oct 12, 2016 at 22:45 DoraDora 6,97015 gold badges56 silver badges116 bronze badges 3
  • 3 No. If you know what shift() does - And that's executing a function from your array and removing it from the Array stack. – Roko C. Buljan Commented Oct 12, 2016 at 22:48
  • 1 shift() removes the first element in the array and returns it. It's inefficient though for what you're doing. – castletheperson Commented Oct 12, 2016 at 22:51
  • @4castle omg! thx thx I totally forgot that it returns the element that it doesn't just remove it. totally got it thx thx a lot – Dora Commented Oct 12, 2016 at 22:56
Add a comment  | 

2 Answers 2

Reset to default 15

So you're basically asking what shift does and here you go:
What you can do using for(var i=0;... you can do using shift() (quite similar but not!)

Using for loop (and index)

var array = [
  function(){return "a";},
  function(){return "b";}
];
  
for(var i=0; i<array.length; i++){
    console.log( array[i]() );  
    // "a"
    // "b"
}

console.log(array.length); //2       !!Still there!!!

Using shift() (and while for example)

var array = [
  function(){return "a";},
  function(){return "b";}
];
  
while(array.length){                // while array has length
    console.log( array.shift()() );  // execute and remove from array
    // "a"
    // "b"
}

console.log(array.length); //0   !!!Empty array due to shift()!!!

So basically it removes a key from your Array and returns it.
As long as that array has keys it will loop until it's empty.

The difference between the two is drastic:
The for loop in example 1. will loop but not alter your original array.
Using shift() in example 2. you're (using and) removing your Array keys one by one.

Read more about Array manipulation:

Array.prototype.shift 1 <-- [2,3,4]
Array.prototype.unshift 5 --> [5,2,3,4]
Array.prototype.push [5,2,3,4,6] <-- 6
Array.prototype.pop [5,2,3,4] --> 6

and other Methods

You can simplify the logic using Array.reduce

Here how you can do that:

var puzzlers = [
function(a) { return 8 * a - 10; },
function(a) { return (a - 3) * (a - 3) * (a - 3); },
function(a) { return a * a + 4; },
function(a) { return a % 5; }
];

function runPuzzler(inputValue){
   return puzzlers.reduce(function(prev, curr){
       return curr(prev);
   },inputValue);
}

Output :

EachArrayValues::100
EachArrayValues::200
EachArrayValues::0
EachArrayValues::400
EachArrayValues::500
EachArrayValues::-50
Negative value found, ie -50
发布评论

评论列表(0)

  1. 暂无评论