JavaScript Get array values in pairs javascript

so I have an array in which I store values in pairs. I can't use an object because keys may repeat so I use an array instead.

This is how the array may look:

array = ['id1', 'id5', 'id9', 'id12', 'id2', 'id9', 'id1', 'id4'];

So I want to iterate through it and get all the pairs, so I can use them for another function:

var a = 'id1';
var b = 'id5';    
doStuffWithThePair(a, b);

var a = 'id9';
var b = 'id12';    
doStuffWithThePair(a, b);

var a = 'id2';
var b = 'id9';    
doStuffWithThePair(a, b);

var a = 'id1';
var b = 'id4';    
doStuffWithThePair(a, b);

I tried with for loops, using if(i%2 == 0) but it doesn't work. How can I make this? Thanks in advance!

Answer:1

You can iterate through your array like this:

function doStuffWithThePair(a, b) {
    // your code is here.
    console.log(a, b);
}

var array = ['id1', 'id5', 'id9', 'id12', 'id2', 'id9', 'id1', 'id4'];
for (var i = 0; i < array.length - 1; i += 2) { 
    doStuffWithThePair(array[i], array[i + 1]);
}
Answer:2

I have the following jQuery code: function next() { //some code here } function previous() { //some code here } $("#next").click(function(){ next(); }); $("#previous").click(function(){...

I have the following jQuery code: function next() { //some code here } function previous() { //some code here } $("#next").click(function(){ next(); }); $("#previous").click(function(){...

  1. what does jquery click return
  2. how does jquery click work
  3. does jquery click work on mobile
  4. does jquery click override onclick
  5. jquery click doesn't work
  6. jquery click does nothing
  7. jquery click does not fire

I'm trying to make a function which is similar to React's createClass function. It should take a POJO and turn it into constructor function which can be invoked with additional arguments. Here's the ...

I'm trying to make a function which is similar to React's createClass function. It should take a POJO and turn it into constructor function which can be invoked with additional arguments. Here's the ...

  1. strange bird behavior
  2. strange bird behavior 2019
  3. strange bird behavior 2018
  4. strange bird behavior window
  5. strange wild bird behavior
  6. love bird strange behavior

I'd like to replace javascript's built in alert() function with a React Component that handles the same task: namely, showing a quick, dismissible message to the user. Now, I can make this happen by ...

I'd like to replace javascript's built in alert() function with a React Component that handles the same task: namely, showing a quick, dismissible message to the user. Now, I can make this happen by ...

  1. react dynamically insert component

I have a React components. I want to test them. But I get unexpected error on every trying to findRenderedDOMComponentWithTag with tested component. Here is the Karma error log: 05 09 2015 20:31:23....

I have a React components. I want to test them. But I get unexpected error on every trying to findRenderedDOMComponentWithTag with tested component. Here is the Karma error log: 05 09 2015 20:31:23....

  1. reactjs state undefined in function
  2. reactjs undefined is not a function
  3. react js function undefined
  4. react state undefined in function