# Array question in JS

**URL:** <https://discourse.processing.org/t/array-question-in-js/5875>\
**Category:** Libraries\
**Created:** [November 24, 2018, 3:54am UTC](https://discourse.processing.org/t/array-question-in-js/5875 "2018-11-24T03:54:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![flamo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/flamo/32/1576_2.png) [@flamo](https://discourse.processing.org/u/flamo)\
**Post date:** [November 24, 2018, 3:54am UTC](https://discourse.processing.org/t/array-question-in-js/5875/1 "2018-11-24T03:54:18Z")

</div>

I’m building a p5 library and I’m new to JavaScript. I’m confused by the JavaScript closure and array system. I don’t understand why I get undefined object “field” in for loops.

Ps: I’m using the math.js eval to translate String to function.

Here is my code

```auto
p5.prototype.vectorField = function (fx,fy,size){

    let field = new Array();

    // this is correct.
    console.log(Array.isArray(field));

    if( typeof fx == "string" ){
        for(let i = 0; i < size; i++){
            let scopex = {x:i};
            let x = math.eval(fx,scopex);
            for(let j = 0; j<size; j++){
                let scopey = {y:j};
                let index = i+(j*size);
                let y = math.eval(fy,scopey);

		// but I get error here!
                if (!(index in field)){

		// and here!
                 field.push([]);
                }
                field[index].push(p5.Vector(x,y,0));
            }
        }
    }

    console.log(field);
    return field;
};

```

And it throws error like this : “Object doesn’t support property or method ‘push’”

---

<div class="post-metadata">

**Author:** ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)\
**Post date:** [November 24, 2018, 4:10am UTC](https://discourse.processing.org/t/array-question-in-js/5875/2 "2018-11-24T04:10:03Z")

</div>

> [@flamo](#):
>
> field = field.push();

I don’t think you have to reassign `field`; it should only be:

```auto
field.push([]);

```

---

<div class="post-metadata">

**Author:** ![flamo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/flamo/32/1576_2.png) [@flamo](https://discourse.processing.org/u/flamo)\
**Post date:** [November 24, 2018, 4:13am UTC](https://discourse.processing.org/t/array-question-in-js/5875/3 "2018-11-24T04:13:05Z")

</div>

Yes you’re right. but it still gives the same error.

---

<div class="post-metadata">

**Author:** ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)\
**Post date:** [November 24, 2018, 4:26am UTC](https://discourse.processing.org/t/array-question-in-js/5875/4 "2018-11-24T04:26:18Z")

</div>

Can I ask you to specify which error? You have two.

If it was the first error, I think you’re misusing the `in` operator. Looking at [the docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in) here, it seems that it only applies to dictionaries (although if you’re coming from Python I understand the confusion). However, this is the first time I’ve ever heard of the `in` operator, so correct me if I’m wrong.

---

<div class="post-metadata">

**Author:** ![flamo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/flamo/32/1576_2.png) [@flamo](https://discourse.processing.org/u/flamo)\
**Post date:** [November 24, 2018, 4:36am UTC](https://discourse.processing.org/t/array-question-in-js/5875/5 "2018-11-24T04:36:07Z")

</div>

I don’t think it’s the in operator causes this. It works as an array length tester(there is an example it in the doc) Also I tried to delete the " if( … to … )" line then the push() error just pop up.

I noticed the variable"field" just lost its Array property after two iterations disregarding if it is empty or not.

---

<div class="post-metadata">

**Author:** ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)\
**Post date:** [November 24, 2018, 4:54am UTC](https://discourse.processing.org/t/array-question-in-js/5875/6 "2018-11-24T04:54:05Z")

</div>

Hm, if this doesn’t work, I’ll give it a go on my own computer, but perhaps try changing field into a (less) local variable by declaring it as a var? I don’t know what the nuances of `let` are, but it’s worth a try…

EDIT: Look like `let` is super-local (for lack of a better term

> **[let](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let)**
>
> The let statement declares a block scope local variable, optionally initializing it to a value.

EDIT-EDIT: Nvm. I’m dumb. Still, try changing it to `var`

---

<div class="post-metadata">

**Author:** ![flamo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/flamo/32/1576_2.png) [@flamo](https://discourse.processing.org/u/flamo)\
**Post date:** [November 24, 2018, 5:14am UTC](https://discourse.processing.org/t/array-question-in-js/5875/7 "2018-11-24T05:14:44Z")

</div>

Um. I’ve tried it. No luck. I’ve been stuck here for two days when I tried and learned many things. 😂

If you don’t mind to try it, you might need the [math.js](http://mathjs.org/download.html) to get it run.

Thank you and cheers!

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [November 24, 2018, 6:57am UTC](https://discourse.processing.org/t/array-question-in-js/5875/8 "2018-11-24T06:57:06Z")

</div>

Notice the math.js library is not in question here. You can remove it from your provided code so to focus in your actual function.

If you want to manage an array of objects with a JSON structure consider checking [JSONArray](https://processing.org/reference/JSONArray_append_.html).

To revise functionality of the `in` operator and `push()` function in Javascript, you can check their respective docs: [in](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in) and [push](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push). Important to note that `push()` does not return the new array but the new size.

Your question is really not based on the paradigm of closures as you have states in the title. You should edit your question so to reflect the content of your post. You are working with arrays and pushing values to it… properly.

One sample code that could be useful to you is shown below.

Kf

```auto

function setup() {

  let size = 10;
  let field =[]; 

  for (let i = 0; i < size; i++) {
    field.push([0]);
    let x = i;
    for (let j = 0; j<size; j++) {

      let index = i+(j*size);
      index*45
        let y = j;

      if ((field[x]==null)) {
        field[x] = new Array();
      }
      field[x][y] = ([x, y, 0]);
      console.log(index + " ==> " +field.length + ' : ' + field[x][y]);
    }
  }

  console.log(field);
}

function draw() {
}

```

---

<div class="post-metadata">

**Author:** ![flamo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/flamo/32/1576_2.png) [@flamo](https://discourse.processing.org/u/flamo)\
**Post date:** [November 25, 2018, 5:54am UTC](https://discourse.processing.org/t/array-question-in-js/5875/9 "2018-11-25T05:54:20Z")

</div>

your code works thanks. I thought it was somehow a compatibility and closure issue, because it works when I tested it alone. Sorry about that, I’m a newbie in JS.

But I still don’t quite understand why my .push([]) (without a return) doesn’t work there. because, when I do this it works fine:

```auto
let a = new Array();
a.push([]);
a[0] = ['cat', 'dog'];

```
