# Arrays

\*\*\*\* **NOTE: BLOG MOVED TO** [**https://cmrodriguez.me/**](https://cmrodriguez.me/) **\*\*\*\***

Arrays are transformed in Frida from Javascript to Java transparently, so there is no special consideration, as shown in the following examples:

```javascript
var intArray = [1, 2, 3];
console.log(ArrayType.sumArray(intArray));
```

This example shows a transformation from a javascript Array to a Java array.

```javascript
ArrayType.sumArray.overload("[I").implementation = function (arrayList) {
    var total = 0;
    for (var i = 0; i < arrayList.length; i++) {
        total += arrayList[i];
    }
    console.log("Entra en arrayInt sumArray: " + total);
    return total;
}
```

This method receives an array in Java. When the Frida user writes the reimplementation function, they will receive a Javascript Array.

{% hint style="danger" %}
The forEach structure (used in Javascript) to iterate on an array does not work.
{% endhint %}

Whenever the following code is called:

```javascript
var total = 0.0;
arrayList.forEach(function (element) { total += element; });
```

the frida server generates an error:

```javascript
TypeError: undefined not callable (property 'forEach' of [object Object])
    at [anon] (../../../frida-gum/bindings/gumjs/duktape.c:65012)
    at /examples.js:278
    at input:1
```

Working with array of Objects is like working with native types, with the exception that a position in the Array can be null, so it must be taken in consideration when the script is being developed, as in the following example:

```javascript
var peopleArray = ArrayType.getAllPeople();
for (var i = 0; i < peopleArray.length; i++) {
    if (peopleArray[i] == null) {
        console.log(i + " - null");
    } else {
        console.log(peopleArray[i].getId()+" - "+peopleArray[i].getName() + " - " + peopleArray[i].getAge());    
    }
}
```

{% hint style="info" %}
A null value in the Java array is translated to a null javascript value automatically by the framework.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://neo-geo2.gitbook.io/adventures-on-security/frida-scripting-guide/arrays.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
