0

I've created this large Array of square objects in my Vue Instance, that look like this:

new Vue({
el: '#application',
data:
{
    title : 'Conways Game of Life',
    squaresClassVar : 'squares',
    userColor : '#453BCC',
    startBool : true,
    squaresFromServer:[],
    squares:
    [
        { row: 0, column: 0, color:'#D3D3D3'},
        { row: 0, column: 1, color:'#D3D3D3'},
        { row: 0, column: 2, color:'#D3D3D3'},
        { row: 0, column: 3, color:'#D3D3D3'},
        { row: 0, column: 4, color:'#D3D3D3'},
        { row: 0, column: 5, color:'#D3D3D3'},
        ...
     ]

Each square is tied to an actual square canvas in my HTML and CSS. When I make a request to the server I'm populating the squaresFromServer[] Array which gets populated just like the squares array. I'm trying to loop through both arrays to update the colors of the squares that are sent from the server.

the current algorithm looks like this:

this.squares.forEach(indexLocalSquares => {
    this.squaresFromServer.forEach(indexServerSquares => {
        if(indexLocalSquares.row == indexServerSquares.row  && indexLocalSquares.column == indexServerSquares.column )
        {
            console.log('entered')
            indexLocalSquares.color = indexServerSquares.color 
        }
        else
        {
            indexLocalSquares.color = '#D3D3D3'
        }
    });
});

The problem is indexLocalSquares.color is not actually updating with the color that is sent from the server's square.

Does anyone know how I might loop through both of these arrays so that I could update the color of the squares Array with the squares that are sent from the server.

In most other languages I would simply do something like this:

for(int i = 0, i <= squares.length - 1, i++)
{
    for(int j = 0, j <= squaresFromServer.length - 1, j++)
    {
        if(squares[i].row == squaresFromServer[j].row && squares[i].column == squaresFromServer[j].column)
        {
            squares[i].color == squaresFromServer[j].color
        }
    }
}
7
  • Please note that data must be a function Commented Jul 3, 2018 at 17:29
  • @Derek data must be a function when defining a component, it can be a simple object when initializing a Vue instance. Commented Jul 3, 2018 at 17:38
  • @EmileBergeron would you mind pointing me to a place in the documentation where I might find that Commented Jul 3, 2018 at 17:47
  • @Derek any example in the doc really, they all use the simpler syntax of instantiating Vue with new Vue(/*...*/) with a data object. And the API entry on the data option is clear about this. Commented Jul 3, 2018 at 17:52
  • @EmileBergeron the API description reads more as a warning than anything else Commented Jul 3, 2018 at 17:54

1 Answer 1

1

You are overwriting the color in different iterations of the cycle. First set all colors to your default color and then update only those colors which are present in the server response

this.squares.forEach(indexLocalSquares => {
            indexLocalSquares.color = '#D3D3D3'
});

this.squares.forEach(indexLocalSquares => {
    this.squaresFromServer.forEach(indexServerSquares => {
        if(indexLocalSquares.row == indexServerSquares.row  && indexLocalSquares.column == indexServerSquares.column )
        {
            indexLocalSquares.color = indexServerSquares.color 
            console.log(indexLocalSquares);
        }
    });
});

Please note that nested iteration is of O(rows * cols) complexity.

1
  • Thank you, that makes so much sense now from reading your answer. I feel kinda sheepish for doing that. But I struggled for a quite a long time and it made me question my syntax using Vue. Thanks again.
    – Paul Mayer
    Commented Jul 3, 2018 at 21:48

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.