0

I have a question about the following problem:

I made a board for a game made out of pieces. Each piece has its own x and y coordinates, but also a boolean wether it's a road or not and wether it has a turret on or not.

So this is the Array:

var Array = [""];            
Array.push({
          x: i, 
          y: j,
          road: false,
          tower: null,
          id: "gamezone"+j+i
        });

Now, the Array.push is found in a loop which gives the information: that part works. Now for the question:

How do I edit certain information of each object of the array? For instance, certain pieces do have a road on, so what I did was:

Array.push({road: true});

but that didn't do it. How would I do that?

Second question: If I want to use some information of objects of the array, for example I have to make an if-else with if (Array(road = true)) then ... I don't think that's a correct way to get info out of the array, so please help me there.

If you've any documentation on doing this, mind giving a link as well?

Thanks a bunch!

Kryptonous
  • 99
  • 1
  • 2
  • 11
  • 1
    Using the name `Array` as a variable name is a bit confusing since `Array` is also the JavaScript type of `[]`. – Davin Tryon May 18 '15 at 13:42
  • `Array` is a reserved term for actual array objects. A variable with that name overwrites it for that scope, which can lead to oddities later in your code. I highly recommend to change the variable name. – Tim S. May 18 '15 at 13:44
  • I just used it here as an example. I don't use it normally. – Kryptonous May 18 '15 at 14:42
  • 1
    For a board game a 2-dimensional array would be a better data structure: `var board = []; for(var i = 0; i < n; i++) {board[i] = []; for(var j = 0; j < n; j++) {board[i][j] = {}}}`, then you can use `board[x][y]` to get/set values. – Pavel Gatnar May 18 '15 at 14:56
  • @PavelGatnar : would you mind editing this fiddle? [link](https://jsfiddle.net/m654rf0s/) with the 2dimensional array? I think I do understand, but it'd be nice if I know it works. – Kryptonous May 18 '15 at 15:11
  • 1
    updated https://jsfiddle.net/m654rf0s/1/ (console rows commented out) – Pavel Gatnar May 18 '15 at 15:22
  • Works like a charm! And way better to find my indexes in! Thank you so much. @PavelGatnar – Kryptonous May 18 '15 at 15:27
  • tip: for the map use string, example for 4x4: `var lvl1 = 'R000-RR00-0R00-0RR0'`, where `R` means road, `0` nothing, `-` next line; you can use other letters for other objects. then you get 2-dimensional array of characters using `var map1 = lvl1.split('-').map(function(x){return x.split('')})` and you can initiate the map based on `map1[i][j]` – Pavel Gatnar May 18 '15 at 15:49
  • Mind joining the chat @PavelGatnar - [Link to Chatroom](http://chat.stackoverflow.com/rooms/78101/discussion-between-kryptonous-and-pavel-gatnar) – Kryptonous May 18 '15 at 16:47

4 Answers4

1

to change the values you can create a new array using the map function:

var arr = []
arr.push(someObj)
arr = arr.map(function(x, i){
  if(someCondition) {
    x.road = true
  }
  return x
})

to get the values use the filter function:

var arr = []
arr.push(someObj)
var sel = arr.filter(function(x, i){
  return someCondition
})
Pavel Gatnar
  • 3,987
  • 2
  • 19
  • 29
1

First of all change the variable's name. And, also, variables that begin with a capital letter are used for constructor functions, by convention.

push() adds a new element to the end of the array. When you are trying to change one property of one of the array's elements you shouldn't be using push. If you want to change one object element in the array you first have to find it. And then change its property.

aray[indexOfObject] will find the object. You must know the numerical index the object is at. If you would like to reference the object using named keys (not numerical keys), you should use one object to hold all the others. But if you know the index, that is how you proceed:

aray[indexOfObject].road = true;
bpbutti
  • 381
  • 1
  • 8
  • I tried this one, although it did work, but not completely.Could you check the fiddle I made: [link](https://jsfiddle.net/m654rf0s/)? As you might notice, the brown pieces should have road = true, the green ones false. But in the console you can see that that's not always right... @bpbutti – Kryptonous May 18 '15 at 15:07
1

you try push the object into the object array, so whenever you try to access or update any array element of these array, you need get it by index of array like-

Array[0]

it gives you object at the 0 index in array Array also whenever you want to update some array item you need to find it's index first of all and then updated that array item, so for find the index corresponding search item you need to custom find function. it also possible using indexOf() of array object. like- Array.indexOf(ArrayItem); and update array item like following-

Array[0]={
          x: i, 
          y: j,
          road: false,
          tower: null,
          id: "gamezone"+j+i
        };

so try with first find the index of array element which you want to update and then update that array item.

Nilesh Nartam
  • 306
  • 2
  • 11
  • Hey there! This almost worked, but for a strange reason it isn't always right. Mind checking my fiddle? [link]((https://jsfiddle.net/m654rf0s/)? @Niles – Kryptonous May 18 '15 at 15:09
0

According to this Loop through an array in JavaScript

you can iterate your array and check/modify properties

for (var i = 0; i < yourArray.length; i++) {
    if (yourArray[i].x == 1 && yourArray[i].y == 2)
    {
       // Do something
    }
}
Community
  • 1
  • 1
suvroc
  • 3,058
  • 1
  • 15
  • 29