Urgent.News

What's breaking now, across thousands of outlets.

Tech

Nested Arrays in JavaScript

Introduction When working with arrays in JavaScript, we usually start with a simple list of values. This works well when all the values belong to the same group. Sometimes, we may want to group those values while still keeping them in a single array. For example: const numbers = [ [ 10 , 20 , 30 ], [ 40 , 50 , 60 ] ]; Here, the numbers are organized into two groups. Each group is an array, and…

In JavaScript, arrays are commonly used to store a collection of values. While simple arrays can contain elements of the same type, sometimes it's beneficial to organize values into groups within a single array. This is achieved by creating nested arrays.

A nested array consists of multiple arrays, each containing its own set of values. For instance, consider the following example:

```javascript

const numbers = [ [ 10, 20, 30 ], [ 40, 50, 60 ] ];

```

In this case, the `numbers` array holds two groups of numbers. Each group is an array, and both arrays are nested within the `numbers` array. To illustrate this concept further, let's examine a regular array first:

```javascript

const fruits = [ "Apple", "Banana", "Orange" ];

console.log(fruits[0]);

console.log(fruits[1]);

console.log(fruits[2]);

```

The output of this code is:

```

Apple

Banana

Orange

```

Here, each fruit has an index representing its position in the array. In JavaScript, the index starts at 0, so "Apple" is at index 0, "Banana" at index 1, and "Orange" at index 2.

Now, imagine we want to split the fruits into smaller groups:

```javascript

const fruits = [ ["Apple", "Banana"], ["Orange", "Mango"] ];

console.log(fruits[0]);

console.log(fruits[1]);

```

The output becomes:

```

["Apple", "Banana"]

["Orange", "Mango"]

```

In this nested array, the outer array holds two inner arrays, each representing a group of fruits. Accessing data in a nested array requires multiple indices. For example, to access the first group of numbers, we could use:

```javascript

const numbers = [ [10, 20, 30], [40, 50, 60] ];

console.log(numbers[0]);

```

This code outputs:

```

[10, 20, 30]

```

The first index, `0`, retrieves the first inner array. To extract a specific value, like the number 20, we need a second index:

```javascript

console.log(numbers[0][1]);

```

This line outputs:

```

20

```

Breaking it down, `numbers[0]` retrieves the first inner array `[10, 20, 30]`, and `numbers[0][1]` accesses the second value from that array, which is 20.

The concept of nested arrays can be applied to various data types. For example, grouping student names might look like this:

```javascript

const students = [ ["Hana", "Dini"], ["Khalisha", "Khayra"] ];

console.log(students[0][0]);

console.log(students[1][1]);

```

The output is:

```

Hana

Khayra

```

In this example, `students[0][0]` retrieves the first name from the first inner array, "Hana". Similarly, `students[1][1]` retrieves the second name from the second inner array, "Khayra".

To access a value in a nested array, we start from the outermost array and progress inward with each index. The syntax `array[firstIndex][secondIndex]` allows us to navigate through multiple levels of nesting. This approach becomes more intuitive as we gain familiarity with working with nested arrays in JavaScript.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Tango Fuel App Expands to Five African Countries

Emma Okonji Tango Brook Technologies Limited, Nigeria’s fast-emerging energy technology and intelligence company, has expanded its footprints into five African countries within one year of its…

  • Brook Technologies expands Tango App to five African countries in one year
  • Africa Managing Director aims for pan-African presence
  • Company targets 15 African countries by 2027

Every line of my recovery code was correct. It failed every single time."

I run an automated trading system on a paper account. Two pipelines, a handful of small single-purpose agents, no human in the loop once it starts.

  • Recovery code verified correct, system failed each attempt
  • Position left unprotected despite self-healing routine
  • Retry mechanism generated same clientorderid for each attempt

C# Singleton basics

Introduction The Singleton pattern ensures that a class has only one instance throughout an application and provides a single, globally accessible way to retrieve it.

Data Science vs. Data Engineering: Mana Jalur Karier yang Tepat untuk Kamu? - 20:50

Di era digital saat ini, data sering disebut sebagai 'minyak baru'. Namun, layaknya minyak mentah, data tidak akan berguna tanpa proses pengolahan yang tepat.

  • Data Science is interdisciplinary field using stats, algorithms to extract insights from data.
  • Data Engineering builds systems for large-scale data collection and utilization.
  • Collaboration between Data Science and Data Engineering is crucial for innovation.

More from Monday 28 September →