Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7055155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:42:52+00:00 2026-05-28T03:42:52+00:00

Total noob question. In PHP I can easily do this: foreach( $array1 as $key

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:42:53+00:00Added an answer on May 28, 2026 at 3:42 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is surely a total noob question... I'm switching to a new VPS host,
I am a total NOOB in programming (but this is only my second question
I'm sure this is a total noob question and I'm missing a blatant error,
Sorry if this is a total noob question, but I wanted to try to
Hello and thank you in advance. I know this is total noob question, and
Total noob question, but here. CSS .product__specfield_8_arrow { /*background-image:url(../../upload/orng_bg_arrow.png); background-repeat:no-repeat;*/ background-color:#fc0; width:50px !important; height:33px
Hey! Total CakePHP noob here. Updated at bottom / This is sort of a
Total newbie question but this is driving me mad! I'm trying this: myInt =
Total noob question here, but not finding the answer via search. What is the
OK, total noob question, for msysgit on Windows 7, but I have a remote

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.