Discord
Login
Community
DARK THEME

How to take slices from a List

Following Problem: How to take a "sublist" from a list

Like:

test=[0,1,2,3,4,5]
slice=[2,4] 
//OR
slice=[2:4]

// ===> slice should be assigned to [2,3]

Any solution there?

Not quite pure microScript but it's the easiest way I can think of:

system.javascript("""this.slice=function(s,e){this.slice(s,e};""")

test=[0,1,2,3,4,5]
slicedversion=test.slice(2,4)
print(slicedversion)

thanks mate

It turns out that you don't even need to use the Javascript() part:

test=[0,1,2,3,4,5]
slicedversion=test.slice(2,4)
print(slicedversion)

See more details on array functions here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

Yes indeed microScript strings rely on JavaScript strings and JavaScript object functions being available from microScript too, you can generally use any JavaScript string or array operation from microScript (though not documented and not really an official behavior...)

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community