chapbook icon indicating copy to clipboard operation
chapbook copied to clipboard

Arrays - How Do You Manipulate Them?

Open everythingability opened this issue 4 years ago • 2 comments

Hello, not sure if this is a bug. I've looked through the docs, the cookbook and even your source code... Nowhere does it say how do manipulate a list.

If I define an array like this.

myList: []

…then on the next page add to it like this...

myList: myList.push( "banana")

The backstage tells me that myList is 1

…or If I try this…

myList: myList + "banana"

… and then add an apple, backstage tells me myList is "bananaapple"

What's the correct way to add items to an inventory like list. I appreciate I may need Javascript to display the contents of the list, but can I use the passage header to set and get and delete items from an Array. It's so tantalising because underneath my variable in the backstage is the "trails" variable, which is a list of strings, exactly what I want.

I'd prefer not use a Javascript engine state simply because I will be teaching this to students... and that might come later.

Thank you.

everythingability avatar Jun 30 '20 13:06 everythingability

Changing to an enhancement since it’s missing documentation/functionality, not a flaw per se.

klembot avatar Jun 30 '20 19:06 klembot

For anyone wondering. The one "nice" way to do it via the variables is to use javascript spread (...) syntax (if available):

:: Passage 1
storyline : ['intro']
--
This is fist story line ... [[next]]

:: next
storyline: ['next', ...storyline]
--
And the other, where we have a: 🗡️ 
Lets try to [[remove]] something...

:: remove
storyline: [...storyline].filter(x => x !== 'intro')
--
Prosper !!!

michto01 avatar Oct 28 '20 23:10 michto01