如何在 KRL 中对数组进行切片
我在一个数组中有一堆 HTML 片段(谢谢 query()),但我只想使用前五个。我正在使用 foreach
将片段注入到页面中。
如果我的数组是 [0,1,2,3,4,5,6,7,8] 我只想要 [0,1,2,3,4]。在 Python 中我会使用 A[:5]。
如何选择数组的前几个元素并忽略其余元素?
I have a bunch of HTML fragments in an array (thank you query()) but I only want to use the first five. I'm using foreach
to inject the fragments into a page.
If my array was [0,1,2,3,4,5,6,7,8] I would want just [0,1,2,3,4]. In Python I would use A[:5].
How can I select the first few elements of an array and ignore the rest?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 pick() 来实现此目的,但只有当数组中的项目是对象而不是数字或字符串时,它才会正常工作:
在上面的示例中,
b == [{'n' :'c' }, {'n' :'d'}]
我已提交了有关数字和字符串失败的错误。
也可以创建一个递归函数来返回数组的正确切片,但这听起来确实有点痛苦。
You can use pick() for this, but it only appears to work correctly if the items in your array are objects, not numbers or strings:
in the above example,
b == [{'n' :'c'}, {'n' :'d'}]
I've filed a bug about the number and string failures.
It would also be possible to create a recursive function that returned the proper slice of the array, but it does sound a bit painful.