The .slice function

Hi guys, hope you are good !

I have a question about the .slice function. I understand how It works on an array, but how to use It on a nested array. Do you have an example to show me how you can use It in this case please ? Thank you ! :smiley:

a nested array is an array, it’s not different, usage is not different

What i meant is how to index elements in a nested array with the .slice function ?

slice doesn’t act any differently whatsoever as a result of you having arrays in your array

maybe you want to call slice on one of the arrays in your array. kay. do that.
or maybe you want to slice your outermost array. sure, go ahead.
maybe you want to call it on all the subarrays. presumably you would use a loop to step through them.

if you’re suggesting that array.slice would touch other arrays than itself… then no, it doesn’t do that, why would it? it has a specific behaviour, that’s what it’ll do. being nested has zero effect.

if you for example want the third element of an array, then you would do:


regardless of what’s in that array whether that is strings numbers, arrays, twenty five deep nested arrays, whatever, doesn’t matter. you get the third element, that’s it.

I know all of that thank you and my question wasn’t about if .slice has an effect on an other array. What i meant is I can use properly .slice and extract specific elements from the main array but how to use .slice to extract elements from the second array ? Maybe with the following example:


How to just print ‘a’ ?

if you want to slice the second array slice the second array, no?
doesn’t matter whether that is inside another array. it’s an array, it has a slice method

maybe what you’d want to do is create a variable referring to the target that you want to slice.

let hereissomearray = somewhere
hereissomearray.slice(..)  // <- not different

Maybe what you’re really asking is, how do I get a value from an array?
Presumably, what you’re then looking for is indexing. Or maybe iteration.

I have an array with stuff in it. I want the third thing.

let third_item = myarray[2]

I have an array. I wish to slice it.


I don’t want to slice myarray

myarray.slice // <- this slice has nothing to do with it

OKay I get It, I thought that I could do that in less steps, thank you for your help ! :smiley:

For that to be any less steps you would need the outermost array to affect a different array which… it just doesn’t.

Maybe you mean that the variable is a step? It’s not. If you use a variable once you can inline it for the same effect.

To do something with a value you need a reference to that value.

No I meant with less lines of code. I wanted to do that just in one line.

nothing stopping you from combining that into one expression

If you want to add three numbers you could do that in multiple lines of code. But there’s nothing stopping you from reusing the result in the same expression to continue doing more operations.

let a = 1
let b = 2
let c = a + b
let d = 3
let e = c + d

1 + 2 + 3

same situation. both of them carry out the same actions, they both invoke (+) twice.

if you have an expression that gets you an array, then that would let you plug that into a method call, because a method call looks like this:


same as how use of + looks like:


If you just want to get 'a' out of the example array in your screenshot, you don’t need to slice anything. Just use the expression cards[4][0]