1

Total noob question. In PHP I can easily do this:

foreach( $array1 as $key => $value ) {
    $array2[$key] += $value;
}

I cannot figure out a way to do this in javascript... I'm sure there must be a way.

EDIT: It doesn't really matter what I loop over, there should be a generic solution for creating an associative array or object on the fly inside a loop and also dynamically create it's key/value pairs with the option to add up numbers. Maybe the following piece of code will help to understand:

var vat = {};
InvoiceItems.each(function(item){

    vat_rate = item.get('vat_rate');
    vatsum = Number(roundNumber( 100 * item.get('vat'), 2 ) / 100, 2);
    vat[vat_rate] += vatsum;

});

The problem is, this results in an object like this: {"20": "undefined18.00","null":"0.00"}

So there's the "undefined" and also an empty null key/value pair.

2 Answers 2

4

JavaScript does not have such a for-each loop, as defined in PHP. If you're dealing with arrays, you should use a for( ; ; ) loop. Otherwise, if you're dealing with objects, the closest you can get is:

// Assume `object` to exist
var key, value; // Declare variables
for (key in object) {
    value = object[key];
}

Removed part of the original answer, because it seems not relevant to the question

Code, as a response to the comment:

var dom_elements = document.querySelectorAll("input"); // Example, HTMLCollection
var array2 = {};   // Holds counters
for (var i=0; i<dom_elements.length; i++) {
    var element = dom_elements[i]; // Select element
    var key = element.name;        // Example: Elements are grouped by name
    var value = element.value;     // Example: value
    if (key in array2) {
        array2[key] += value;
    } else {                  // If the key does not exist, create one:
        array2[key] = value;
    }
}

Update 2: Based on updated question

Replace vat[vat_rate] += vatsum; with:

if (vat_rate in vat) {
    vat[vat_rate] += vatsum;
} else {
    vat[vat_rate] = vatsum;
}
7
  • I forgot to mention that I need to create the second array (or object) on the fly and that I need to use associatve array (or object) Commented Jan 11, 2012 at 20:59
  • I'm not sure which code block do You mean. The foreach part doesn't really matter, because I am actually looping over a set of dom elements and I need to group similar items together and add up their values. So the "key" for me is really a way to group together similar items and then set the "value" to the sum of all these elements. Commented Jan 11, 2012 at 21:09
  • Group DOM elements, by name/id/...? You're going to use an object, {}. Instead of describing the characteristics of your problem, can you just get to the point, and post the actual question? Commented Jan 11, 2012 at 21:12
  • I now so Your new block of code. Yes, that is what I am looking for, but it doesn't seem to work for me. Instead of array2[index] = value;, I need something like array2[index] += value; but it seems to mess things up, as the first time I'm adding the value, the key is undefined and so the result will be like: undefined2.22, when it should be just 2.22. Also, the array will contain a null: null key/value pair. Commented Jan 11, 2012 at 21:13
  • @ragulka Updated answer based on your latest comments. Commented Jan 11, 2012 at 21:21
0

I think what you are trying to do is this:

var array2 = [4, 5, 6];
var array1 = [1, 2, 3];
for (var key in array1) {
    array2[key] += array1[key];
}

This will add each item in array1 to its corresponding item in array2 and replace the item in array2 with the result, which is essentially what you are doing in your PHP code.

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.