1

I'm trying to get sequential values from a dynamically generated array of numerical values, and add those sequential values together in pairs, in sequence. The generated array will always have an even number of values. As an example, let's say the array comes out to

myArray = [2, 23, 45, 128, 92, 3]. 

How can I use jQuery to iterate over the generated array and get values like this?

val1 = myArray[0] + myArray[1];
val2 = myArray[2] + myArray[3];   
val3 = myArray[4] + myArray[5]; 

The array is dynamically generated, and as the list of items may increase or decrease, I don't want to hardcode existing key/values. I need to come up with a formula that will take into account the future addition or subtraction of the sequential pairs.

Any help would be greatly appreciated!

4 Answers 4

1
var result=[];

for(var i=0,limit=myArray.length; i<limit; i+=2)
{
    result[i]=myArray[i]+myArray[i+1];
}
Sign up to request clarification or add additional context in comments.

Comments

0

I would do it like this,

var newArry = [], tmp, myArray = [1,2,3,4,5];
myArray.forEach(function(v,i){
    if(i%2>0){
        newArry.push(tmp+v);
    }
    tmp = v;
});

console.log(newArry);

3 Comments

Thanks for the answer. It certainly worked in Chrome, but I believe the forEach method isn't properly executed in IE8 and below. I'm trying to repeat your method with a for loop or an $.each() method without success. Any ideas?
same thing with $.each(), jsfiddle.net/2xxkhLqx, foreach is (v,i), $.each it is (i,v)
Merci beaucoup! Great help.
0

You can't use dynamic variable names like that so try

var myArray = [2, 23, 45, 128, 92, 3],
    obj = {};

for (var i = 0; i < myArray.length / 2; i++) {
    obj['val' + (i + 1)] = myArray[i * 2] + myArray[i * 2 + 1]
}

console.log(obj)

Comments

0

Use .each(...) function of a jQuery do a magic for you.

var val = 0;

var myArray = [2, 23, 45, 128, 92, 3];


$.each(myArray, function( index, value ) {

  val = (index % 2 === 0 )? value : val + value; 

  if (index % 2 !== 0 )
    console.log( val); 

});

You can check index value for even / odd and apply your logic here...

PS: The above will show the output in the console box of your browser.

Working Demo: http://jsbin.com/qivesekono/1/

3 Comments

Codebased, thanks for replying. I liked your approach, as I had been trying an "each()/modulus" solution, without success. When I use your code, I'm getting an error- "Uncaught ReferenceError: val is not defined." ? Is it an undeclared variable, or something in the namespace I'm missing?
@user933101 I have modified the code and now you can see the output as desire. Please click on this link: jsbin.com/qivesekono/1
Grazie mille! I appreciate your help.

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.