Javascript arrays

I understand how to do a 1D array in Javascript. Is there a way to do a 2D? Can someone provide an example?

I'm coming up with a game filled with NPC characters (each with a name, statistic, current attitude towards you, etc). I need a way to keep track of all this.

Thanks in advance.


I am not at all familiar with Squiffy, but in JavaScript I would do that as an array of dictionaries, like this:

const characters = [
  { name:"Fred", happy:6, met:false },
  { name:"Mary", happy:4, met:false },
  { name:"Horatio", happy:12, met:false },
]

characters[1].happy = 5;

Just to expand on the excellent answer by The Pixie.
In that answer we are setting Mary's happy score to 5 using:

characters[1].happy = 5;

That is a totally fine way to achieve that and in no way is wrong but it does require the programmer to keep track of the location of the various character information in the array.

The updateHappy function below does that same thing in a way that may be a little more intuitive to use (once the function is written). Hope it makes sense and prompts some even better ideas on how to use the idea in your story.

Please ask if any of it doesn't make sense.

var characters = [
  { name:"Fred", happy:6, met:false },
  { name:"Mary", happy:4, met:false },
  { name:"Horatio", happy:12, met:false },
]

// Defining the function.
function updateHappy(nameToUpdate, newHappyValue){
  for(character in characters){
    if(characters[character].name == nameToUpdate){
      characters[character].happy = newHappyValue;
      break;
    }
  }
}

// Calling the function.
updateHappy('Mary', 5);

Thanks, all. This gives me the missing link between what I was reading on the JavaScript pages and practical applications. With a little effort, a huge list of characters (and items) could easily be constructed.

Thanks!


phm

Hi,
I'm also looking for a way to use arrays and I stumbled on your discussion.
I used your example and tried to get the variable using:
{characters[0].happy}
but I always get:
there is a link to a passage called [0], which doesn't exist

{characters} exists as characters = [object Object],[object Object],[object Object]

So, how do you get a value from the array?
Thank you :)


phm

@jwpfox, any idea?


I can't get arrays with objects to work with the Squiffy code. (I tried every method I could think of.)

If someone gets this working, please post working code for everyone.

Thanks in advance!


Okay, I haven't had too much time to play with it. It looks like you can transfer array values back into squiffy pretty easily. Of course, functions would be better for this - haven't had too much time to play with it.

[[test]]:

    const characters = [
      { name:"Fred", happy:6, met:false },
      { name:"Mary", happy:4, met:false },
      { name:"Horatio", happy:12, met:false },
    ]
    
    characters[1].happy = 5;
    
    set("X",characters[1].happy);
    
X={X}

This seems to work. I think if I wrote it as a function, that would work too.


phm

@Bluevoss thanks! It works.
It looks like we'll juggle with temp vars all the way.
This also mean that writing back to the JavaScript array/object needs a function
... and that the array needs to be in a file,
... writable (met=true),
... JSON I guess.

So we need a common way to read/write to a local JSON file.
If someone has a solution, please feel free to share.
Thanks!


Okay, here's how to do it with functions...

[[test]]:

    var characters = [
      { name:"Fred", happy:6, met:false },
      { name:"Mary", happy:4, met:false },
      { name:"Horatio", happy:12, met:false },
    ]
    
    // Defining the function.
    squiffy.updateHappy=function(nameToUpdate, newHappyValue){
        var character;
        for (character in characters){
            if(characters[character].name == nameToUpdate){
                characters[character].happy = newHappyValue;
                break;
            }
        }
    }
    
    squiffy.fetchHappy=function(nameToFetch){
        var character;
        for (character in characters){
            if(characters[character].name == nameToFetch){
                return(characters[character].happy);
                break;
            }
        }
    }
    
    squiffy.updateHappy('Mary', 10);
    set("X",squiffy.fetchHappy('Mary'));
    
Done
X={X}

Have fun!


phm

Thanks Bluevoss!
Have a great summer everyone.


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums