# Cant write to 2D-Array? Wanna set specific pixel

**URL:** <https://discourse.processing.org/t/cant-write-to-2d-array-wanna-set-specific-pixel/17801>\
**Category:** Coding Questions\
**Created:** [February 13, 2020, 8:48pm UTC](https://discourse.processing.org/t/cant-write-to-2d-array-wanna-set-specific-pixel/17801 "2020-02-13T20:48:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aeh](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@aeh](https://discourse.processing.org/u/aeh)\
**Post date:** [February 13, 2020, 8:48pm UTC](https://discourse.processing.org/t/cant-write-to-2d-array-wanna-set-specific-pixel/17801/1 "2020-02-13T20:48:23Z")

</div>

Hello,  
I wanna try to set specific pixels, but only when the 2d-array position has an “x”. What do I wrong? I cant find the mistake I do ☹

```auto
//this will store the crap-mountain
var data = {};

function preload() {
  data = loadJSON('assets/mountain.json');
}

var mountainTest = Array(100).fill(0).map(() => Array(100).fill("0"));

function setup() {
  createCanvas(100,100);
  background(0);

  mountainTest[50][88].fill("x");// = "x";
  mountainTest[49][98] = "x";  
  mountainTest[50][99] = "x";
  mountainTest[49][99] = "x";
  mountainTest[51][99] = "x";
}

function draw() {
  let black = color(255, 102, 204);
  let img = createImage(100, 100);
  img.loadPixels();
  for (let i = 0; i < img.width; i++) {
    for (let j = 0; j < img.height; j++) {
      //if(mountainTest[i][j] == 'x'){
      //console.log(mountainTest[50][88]);
      img.set(i, j, color(0, 90, 102));
     //}
    }
  }
  img.updatePixels();
  //save(img, 'asstes/mountain.json');
  image(img,0,0);
}

```

Thanks a lot for helping!  
+aeh

---

<div class="post-metadata">

**Author:** ![Napalm1432](https://avatars.discourse-cdn.com/v4/letter/n/e9bcb4/32.png) [@Napalm1432](https://discourse.processing.org/u/Napalm1432)\
**Post date:** [February 14, 2020, 8:52am UTC](https://discourse.processing.org/t/cant-write-to-2d-array-wanna-set-specific-pixel/17801/2 "2020-02-14T08:52:26Z")

</div>

did you forget to remove the comment slashes? ( // )  
I tested your code, and it seems to work fine.  
I did get an error on the first declaration of the mountainTest variable.  
See my “changes” below  
Also, don’t console.log in a for loop like this, it will slow the page down drastically, which might be why you thought it didn’t work

```auto

function setup() {
  createCanvas(100,100);
  background(0);

  mountainTest[50][88] = "x";
  mountainTest[49][98] = "x";  
  mountainTest[50][99] = "x";
  mountainTest[49][99] = "x";
  mountainTest[51][99] = "x";
}
function draw() {
  let black = color(255, 102, 204);
  let img = createImage(100, 100);
  img.loadPixels();
  for (let i = 0; i < img.width; i++) {
    for (let j = 0; j < img.height; j++) {
      if(mountainTest[i][j] == 'x'){
      img.set(i, j, color(100, 100, 102));
     }
    }
  }
  img.updatePixels();
  image(img,0,0);
}

```

---

<div class="post-metadata">

**Author:** ![aeh](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@aeh](https://discourse.processing.org/u/aeh)\
**Post date:** [February 14, 2020, 10:14pm UTC](https://discourse.processing.org/t/cant-write-to-2d-array-wanna-set-specific-pixel/17801/3 "2020-02-14T22:14:33Z")

</div>

Ye thanks!  
It works, tbh… I havent tested it in the Browsers…I just used p5.js canvas in Visual Studio…and thought if It dont work here…it doesnt work in the web…
