简体   繁体   中英

How do I slice an array in KRL

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?

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:

    a = [{'n':"a"},{'n':"b"},{'n':"c"},{'n':"d"}];
    b = a.pick("$[2:]");

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM