Using arrays with Chapbook

If you are requesting technical assistance with Twine, please specify:
Twine Version: 2.3.15
Story Format: Chapbook 1.2.1

I understand that I can use arrays in the var section of Chapbook, but I have no ideas how to manipulate items (for example how to insert a new item in an array (item.push (‘something’) seems not to work.
Or how erase an item, a.s.o.
Thx for your help (sorry, I am a very very beginner in both JavaScript AND Chapbook.


Welcome Castella,

First you should not use preformated text for anything other than code, it’ll be easier to read!

About arrays, if you want to add a member to an array, here’s what I suggest:

array: [array,"New Member"]
--

Like you, I still haven’t found a way to delete a variable, so I’ll let someone else step in.

OK, sorry, I corrected this. Thank You for this first step.

1 Like

@Cedric (and @souppilouliouma )

Unfortunately the above example is not adding the “New Member” item to the current contents of the array variable, it is creating a new Array consisting of two elements:

  • the 1st element contains the contents of the previous array.
  • 2nd element contains the “New Member”.

eg. Assuming you have predefined the following Array…

colours: ['red','blue','green']
--
colours: {colours}

…and you use the following code to add an addition element to the array…

colours: [colours, 'orange']
--
colours: {colours}

…the resulting nested Array will look like the following in the State tab of the Backstage panel.

[["red","blue","green"],"orange"]

note: I couldn’t find a Chapbook specific method for adding / removing Array elements within the Var Section of a Passage, so I will explain how to do it using standard JavaScript Array functions.

The following example used the .push() function to add another element to your existing array variable, using the [JavaScript] modifier…

colours: ['red','blue','green']
--
[JavaScript]
colours.push("New Member");
[continued]
array: {array}

…and the resulting flat Array will look like the following in the State tab of the Backstage panel.

["red","blue","green","orange"]
2 Likes

Chapbook doesn’t have much in the way of facilities for handling arrays–depending on your needs, object variables might work better.

Greyelf’s suggestion will work (and the explanation about nested arrays is spot-on), but here’s a little bit shorter way to add and remove items in an array.

colors: ['red', 'green', 'blue']
colors: [...colors, 'purple']
colors: colors.filter(f => f !== 'blue')
--
{colors}

The reason why push() on an array will not work in a var block is because it sets the result to the total array length (details).

3 Likes

Very good point, @Greyelf, thank you! I had not tested my answer thoroughly, so I apologize to you @Cedric.

I went back to the problem, and I figured the array.flat() method is working.

array: [array,"New Member"]
array: array.flat()
--

Please note that array.flat() only brings the first level of nested arrays at the same level as the non-nested members. If there is a nested array inside a nested array, you may want to add a number (eg array.flat(2)) if you wish to bring those nested arrays already inside in a nested array at the same level as non-nested arrays.

At this point I would offer to look at sugarcube documentation about array.flat(), which I find very clear, but I’m not sure it’d be appropriate for a chapbook question.