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 7973903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:13:52+00:00 2026-06-04T08:13:52+00:00

I have the following loop that adds an object to an array if certain

  • 0

I have the following loop that adds an object to an array if certain criteria are met.

var form = $('form[id^="product-form"]'),
    submit_button = $('#add-to-cart'),
    attr_fields = $('.additional input[type=text]'),
    attr_fields_default = {};
// Get the default values for each attribute field and place into an obj
attr_fields.each(function () {
    var field = $(this),
        field_id = field.attr('id'),
        field_val = field.val();
    attr_fields_default[field_id] = field_val;
});
var extra_attr = [];
for (var i = 0; i < attr_fields.length; i++) {
    var _field = $(attr_fields[i]);
    if (_field.val() != attr_fields_default[_field.attr('id')]) {
        var jsonStr = {
            'add': _field.attr('id'),
            'quantity': 1
        };
        extra_attr.push(jsonStr);
        console.log(jsonStr);
    } else {
        _field.attr('disabled', 'disabled').val('None');
    }
}
console.log(extra_attr);

The code works perfectly until the last loop, although console.log( jsonStr ) outputs the last object as expected, console.log( extra_attr ) does not. It does not push the last item into the array. I even tried changing jsonStr to a dummy string ‘test’ and i still get the same result.

Here is a screenshot of my chrome developer console http://cl.ly/GdHw

I should also note that I originally tried to use .each() to loop through the selectors with the exact same results

         attr_fields
        .each(function(){
          if ( $(this).val() != attr_fields_default[ $(this).attr('id') ] ) {
            var jsonStr = {'add': $(this).attr('id'), 'quantity': 1};
            extra_attr.push( jsonStr );
            console.log( jsonStr );
          } else {
            $(this).attr('disabled', 'disabled').val('None');
          }
  • 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-06-04T08:13:54+00:00Added an answer on June 4, 2026 at 8:13 am

    The problem was with an asynchronous call later in the code that was using .pop() to grab the last element

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following php loop that takes an array that contains a set
I have the following loop that renders an array a of arrays. Each arrays
I have the following code that populates an array (this is within a loop):
I have the following loop: for(var myScreen in wizardScreens){ if(step==index)$(myScreen).show(); else $(myScreen).hide(); index++; }
I have following loop in my jsp page: <c:forEach var=i begin=1 end=${ toLvvt }
Guys, I have the following code that is inside a big while loop that
I have the following loop that simply checks all green rows in a table
I have the following loop in my viewDidLoad: for(int i=1; i<[eventsArray count]; i++) {
I have the following loop inside an function init(); which is executed onload, I
I have the following loop... for ($i = 1; $i <= 10; $i++) {

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.