Nested Arrays

Learn how nested arrays and objects inside arrays work in BornomalaScript.

Nested Arrays

Nested arrays are arrays that contain other arrays or more complex values.

They are useful when your data has more structure than a simple list.

You can use nested arrays to represent grids, grouped values, or objects inside a collection.

Basic Example

Use nested arrays to represent matrices or grids.

dhoro amarArr = [
    "item1",
    "item2",
    {
        nestedObjInsideArr: "hello"
    }
]
lekho(amarArr[2].nestedObjInsideArr)

This example shows one useful pattern:

  • an object stored inside an array

Another Example

You can also store multiple objects in an array and loop through them.

dhoro myArr = [
    {
        name: "mahfuz",
        age: 22,
        role: "admin"
    },
    {
        name: "brixy",
        age: 2,
        role: "assistant"
    }
]

dhoro i = 0
jotokkhon (i < 2) totokkhon {
    lekho(myArr[i].name)
    lekho(myArr[i].age)
    lekho(myArr[i].role)
    i++
}

That pattern is common when you want to store a small list of records.

How It Works

The first example stores a small object in the array and then reads one of its properties.

The second example stores multiple objects in the array and loops through them.

That means nested arrays are useful when one level of structure is not enough.

Why Nested Data Helps

Nested data is helpful when you want to group related values together.

For example, one student record might include:

  • name
  • age
  • role

Instead of making three separate arrays, you can keep the data together in one structured collection.

Step-by-Step Breakdown

  1. The array is created
  2. One element is a text value
  3. Another element is an object
  4. A property is read from the object
  5. A loop prints the values from a collection of objects

This is a useful pattern for beginner data handling.

Try These Variations

You can practice with a simple list of students:

dhoro students = [
    { name: "Mahfuz", age: 22 },
    { name: "Amina", age: 20 }
]

lekho(students[0].name)
lekho(students[1].age)

You can also treat the array like a small table of related values.

Common Mistakes

Beginners often make these mistakes:

  • Forgetting the brackets for arrays
  • Mixing up array indexes and object properties
  • Trying to access a property that does not exist
  • Using the wrong index number

Practice Task

Try creating a nested collection that stores:

  1. A person’s name and age
  2. Another person’s name and age
  3. A third record with the same structure

Then print each name and age from the collection.

Quick Checklist

Before moving on, make sure you can:

  • Store an object inside an array
  • Read a property from the object
  • Loop through multiple records
  • Understand why nested data is useful

If yes, you are ready for array operations.