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

  • Home
  • SEARCH
  • 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 6537093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:34:32+00:00 2026-05-25T10:34:32+00:00

I have an array, lets call it foo. Each element of the array holds

  • 0

I have an array, lets call it foo. Each element of the array holds an object.

Example:

var foo = new Array();

var test = new Object();
test.name = "Item name1";
test.price = 20.00;

foo.push(test);

var test = new Object();
test.name = "Item name2";
test.price = 10.00;

foo.push(test);

I should now have:

foo[0] => object{name: Item name1, price: 20.00}
foo[1] => object{name: Item name2, price: 10.00}

the problem:

console.log(foo.length); // 2

for(var x = 0; x < foo.length; x++) { 
     console.log(foo[x]); // foo[x] is undefined 2X
}

Why is it that I cannot for loop the array of objects and access them as such? I should be able to say foo[x].name (or foo[x]['name']) from within the for loop to get the values but I’m getting undefined! any one know why?

Update:

Since that example is way to simplified, here is the full code of what I’m doing, essentially pkgs has [0] populated at the point of running. A new element (this is an add pacakge function) is added then then values are updated. After that I need to use the new insurance values from the two packages to update the UI with the proper insurance values per-package. The very last comment is the interesting part.

var pk = $('#shipment_'+shipid).data('pkgs'); 
var pkgs = new Array();
for(index in pk) {
    pkgs.push(jQuery.extend(true, {}, pk[index]));
}
var pkgnum = pkgs.length; // always returns one higher than last index.

// add new pkg to array
pkgs[pkgnum] = new Object();
pkgs[pkgnum].weight = weight;

// overwrite packing slip data.
for(var x = 0; x < pkgs.length; x++) {
    var curPS = new Array();
    var curins = 0;
    for(var y = 0; y < shipmentItems.length; y++) {
        var curqty = parseInt($('#pkgqty-'+y+'-'+x).val());
        var nsrow = jQuery.extend(true, {}, shipmentItems[y]);
        curins += curqty * shipmentItems[y]['price'];
        curPS.push(nsrow);
        curPS[y]['qty'] = curqty;
    }
    pkgs[x].packing_slip = curPS;
    pkgs[x].insurance = Math.ceil(curins);
}

// write pkgs data()
$('#shipment_'+shipid).removeData('pkgs');
$('#shipment_'+shipid).data('pkgs', pkgs);  

// update insurance values
console.log(pkgs); // shows two objects
console.log("len: " + pkgs.length); // len is 2
for(var x = 0; x <= pkgs.length; x++) {
    var insuranceHTML = "$"+pkgs[x].insurance+'<a href="javascript:overrideInsurance('+shipid+','+x+');"><img src="/img/edit2.png" height="16" width="16" alt="" title="Override Insurance Value" align="absmiddle" /></a>';
    $('#pkgins-'+shipid+'-'+x).html(insuranceHTML);
    // pkgs[x] == undefined when x = 1 but not when x=0
}
  • 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-25T10:34:32+00:00Added an answer on May 25, 2026 at 10:34 am

    Seems to work just fine:

    http://jsfiddle.net/JyLD4/1/

    Are you sure this is the issue?

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

Sidebar

Related Questions

I have a java object (let's call it Foo ) with a length() method.
I have an initialised array that may contain no items. Lets call it a
I have found this example on StackOverflow: var people = new List<Person> { new
My approach: An array of fixed-length (lets say 20) each element is pointer to
I have such a list List<Double[,]> . Let's call each 2-dimensional array in the
I have this array lets call it array 1 Array ( [0] => Array
I have a decimal number (let's call it goal ) and an array of
Lets say I have an array like this: string [] Filelist = ... I
Lets say I have this array, int[] numbers = {1, 3, 4, 9, 2};
I have a bunch of Python functions. Let's call them foo , bar and

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.