jQuery Create Multidimensional Array

jQuery Create Multidimensional Array

  1. Blog
  2. 15 November 2026
  3. 2 min read

Stefan Bogdanescu

Founder & Senior Architect

Why myArray[i][$row[sku]] Fails and How to Fix It

You are encountering the error Cannot set property '295518' of undefined because you are attempting to assign a value to an index that does not exist on the first dimension of your array. In JavaScript, arrays are objects where numeric indices represent elements. When you write myArray[i], you are accessing a specific slot. If that slot has not been initialized as an array itself, it is currently undefined.

Trying to access a property (like $row[sku]) on undefined results in the immediate crash you are seeing. The error message explicitly tells you this: the second dimension (295518) cannot be attached because the first dimension (i) hasn't been defined yet.

The Initialization Gap

In your loop, you initialize myArray as an empty array before the loop starts:

var myArray = [];

This creates a container with no elements. Inside the loop, you iterate through rows, but you assume that myArray[i] already exists and is ready to accept data. It does not.

Consider what happens on the first iteration where i equals 0. Your code attempts to execute:
myArray[0][$row[sku]] = $row[qty];

At this exact moment, myArray[0] evaluates to undefined. JavaScript cannot create a nested object structure automatically just by assigning properties; you must explicitly tell it that the slot at index i is an array before you try to push items into it.

The Solution: Conditional Initialization

To resolve this, you need to check if the element at index i exists. If it does not, you must initialize it as a new empty array before assigning the property. This pattern ensures that every time you enter the loop with a new row index, the structure is ready for data.

Here is the corrected logic:

var myArray = [];

// Assuming i is your loop counter and $row is your data object
for (let i = 0; i < rows.length; i++) {
    const sku = $row[sku];
    const qty = $row[qty];

    // Check if the current row index exists in the main array
    if (!myArray[i]) {
        myArray[i] = [];
    }

    // Now safely assign the value to the nested index
    myArray[i][sku] = qty;
}

console.log(myArray);

Handling Duplicate SKUs

You specifically asked if repeating a SKU (e.g., "295518") would cause an issue. The answer is yes, but not in the way you might fear. In JavaScript objects—which arrays are—keys must be unique within that specific scope.

If myArray[i] is initialized as an array, and you try to run myArray[i]["295518"] = 100 twice, the second assignment simply overwrites the first value. It will not throw a TypeError. However, this data loss might be your actual problem if you are aggregating quantities for different products that happen to share an ID, or if your logic expects distinct entries.

If duplicates in the SKU column represent distinct records (like multiple stock movements for the same item) and you want to keep them separate, a multidimensional array indexed by string SKUs is likely the wrong data structure. You would be better off iterating over all items and pushing them into a single flat array with metadata, or using an object where keys are unique strings if collisions are acceptable via overwrite.

A More Idiomatic Approach in Modern JS

While fixing the initialization issue solves your immediate crash, relying on string keys for numeric indices in nested arrays is often a code smell. If sku values are purely integers, JavaScript treats them as property names, but mixing types can be confusing.

If you are working within a Laravel environment or building complex data structures, consider using Array.prototype.reduce to build this structure in one pass without manual index management. This approach is less error-prone because it doesn't rely on guessing whether an index exists:

const myArray = rows.reduce((acc, row) => {
  const sku = row.sku;
  const qty = row.qty;
  
  if (!acc[sku]) {
    acc[sku] = [];
  }
  
  // If you need to preserve order of i, this approach requires a map first.
  // But often, grouping by SKU is sufficient:
  acc[sku].push(qty); 
  return acc;
}, {});

This reduces the cognitive load of managing loop counters (i) and existence checks manually. Just ensure you are comfortable with the trade-off between strict array indexing and object keying for your specific use case.

Our network