LWC: detect last element in for:each iterationAura iteration too show value in two columnsLWC-Jest installation failsCan we See the lwc Module?Handle click outside element in LWCHow do you define an element with an ID attribute using LWC?LWC: Use data from outer iteration in inner iteration to conditionally render?LWC: Invalid event type “WireContextEvent” dispatched in elementLWC: Display HTML Contentonchange event not triggered on removal of last character - LWCLWC: Using “if” to condition CSS class dynamically in LWC

When editor does not respond to the request for withdrawal

Why is my Taiyaki (Cake that looks like a fish) too hard and dry?

David slept with Bathsheba because she was pure?? What does that mean?

Is there a frequency comparator device?

How can religions without a hell discourage evil-doing?

Is it possible to have battery technology that can't be duplicated?

Part of my house is inexplicably gone

Is it good practice to create tables dynamically?

Parsing text written the millitext font

Can I attach a DC blower to intake manifold of my 150CC Yamaha FZS FI engine?

How to represent jealousy in a cute way?

Can you open the door or die? v2

ISP is not hashing the password I log in with online. Should I take any action?

Fastest way from 8 to 7

In American Politics, why is the Justice Department under the President?

Is plausible to have subspecies with & without separate sexes?

Purpose of cylindrical attachments on Power Transmission towers

What's the relation between у.е. to USD?

Simple log rotation script

What's the difference between DHCP and NAT? Are they mutually exclusive?

Why would a home insurer offer a discount based on credit score?

Can I get a photo of an Ancient Arrow?

What is the language spoken in Babylon?

In Pandemic, why take the extra step of eradicating a disease after you've cured it?



LWC: detect last element in for:each iteration


Aura iteration too show value in two columnsLWC-Jest installation failsCan we See the lwc Module?Handle click outside element in LWCHow do you define an element with an ID attribute using LWC?LWC: Use data from outer iteration in inner iteration to conditionally render?LWC: Invalid event type “WireContextEvent” dispatched in elementLWC: Display HTML Contentonchange event not triggered on removal of last character - LWCLWC: Using “if” to condition CSS class dynamically in LWC






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I'm trying to output a list of strings separated by commas in LWC, such as:




cat, dog, mouse




I need to use template for:each and output a list item and a comma, but only if the item is not the last in the list. But I'm having trouble since LWC does not support comparison operations in templates. I tried to set a field in JS to indicate which element is the last:



HTML:



<template for:each=animals.data for:item="animal">
<b key=animal.Id>animal.Name</b>
<template if:false=animal.isLast>
,&nbsp;
</template>
</template>


JS:



@wire(getAnimals)
assignAnimals(animals)
this.animals = animals;
if (this.animals.data && this.animals.data.length > 0)
this.animals.data[this.animals.data.length - 1].isLast = true;




But I cannot assign to this.animals.data[this.animals.data.length - 1].isLast because JS Proxy is guarding it.



TypeError: "proxy set handler returned false for property '"isLast"'"


The other options would be to create isLast field in the controller, but I would need to create a wrapper class.
Another possibility would be some CSS tricks, but I don't want to do that.



Is there any simple way to do this?










share|improve this question






















  • the "proxy" you get is immutable, you can however, create a new array based on the proxy and then do a Join by ',' to output your desired string.

    – glls
    7 hours ago

















1















I'm trying to output a list of strings separated by commas in LWC, such as:




cat, dog, mouse




I need to use template for:each and output a list item and a comma, but only if the item is not the last in the list. But I'm having trouble since LWC does not support comparison operations in templates. I tried to set a field in JS to indicate which element is the last:



HTML:



<template for:each=animals.data for:item="animal">
<b key=animal.Id>animal.Name</b>
<template if:false=animal.isLast>
,&nbsp;
</template>
</template>


JS:



@wire(getAnimals)
assignAnimals(animals)
this.animals = animals;
if (this.animals.data && this.animals.data.length > 0)
this.animals.data[this.animals.data.length - 1].isLast = true;




But I cannot assign to this.animals.data[this.animals.data.length - 1].isLast because JS Proxy is guarding it.



TypeError: "proxy set handler returned false for property '"isLast"'"


The other options would be to create isLast field in the controller, but I would need to create a wrapper class.
Another possibility would be some CSS tricks, but I don't want to do that.



Is there any simple way to do this?










share|improve this question






















  • the "proxy" you get is immutable, you can however, create a new array based on the proxy and then do a Join by ',' to output your desired string.

    – glls
    7 hours ago













1












1








1








I'm trying to output a list of strings separated by commas in LWC, such as:




cat, dog, mouse




I need to use template for:each and output a list item and a comma, but only if the item is not the last in the list. But I'm having trouble since LWC does not support comparison operations in templates. I tried to set a field in JS to indicate which element is the last:



HTML:



<template for:each=animals.data for:item="animal">
<b key=animal.Id>animal.Name</b>
<template if:false=animal.isLast>
,&nbsp;
</template>
</template>


JS:



@wire(getAnimals)
assignAnimals(animals)
this.animals = animals;
if (this.animals.data && this.animals.data.length > 0)
this.animals.data[this.animals.data.length - 1].isLast = true;




But I cannot assign to this.animals.data[this.animals.data.length - 1].isLast because JS Proxy is guarding it.



TypeError: "proxy set handler returned false for property '"isLast"'"


The other options would be to create isLast field in the controller, but I would need to create a wrapper class.
Another possibility would be some CSS tricks, but I don't want to do that.



Is there any simple way to do this?










share|improve this question














I'm trying to output a list of strings separated by commas in LWC, such as:




cat, dog, mouse




I need to use template for:each and output a list item and a comma, but only if the item is not the last in the list. But I'm having trouble since LWC does not support comparison operations in templates. I tried to set a field in JS to indicate which element is the last:



HTML:



<template for:each=animals.data for:item="animal">
<b key=animal.Id>animal.Name</b>
<template if:false=animal.isLast>
,&nbsp;
</template>
</template>


JS:



@wire(getAnimals)
assignAnimals(animals)
this.animals = animals;
if (this.animals.data && this.animals.data.length > 0)
this.animals.data[this.animals.data.length - 1].isLast = true;




But I cannot assign to this.animals.data[this.animals.data.length - 1].isLast because JS Proxy is guarding it.



TypeError: "proxy set handler returned false for property '"isLast"'"


The other options would be to create isLast field in the controller, but I would need to create a wrapper class.
Another possibility would be some CSS tricks, but I don't want to do that.



Is there any simple way to do this?







javascript lightning-web-components iteration






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 8 hours ago









mmm321321mmm321321

32029




32029












  • the "proxy" you get is immutable, you can however, create a new array based on the proxy and then do a Join by ',' to output your desired string.

    – glls
    7 hours ago

















  • the "proxy" you get is immutable, you can however, create a new array based on the proxy and then do a Join by ',' to output your desired string.

    – glls
    7 hours ago
















the "proxy" you get is immutable, you can however, create a new array based on the proxy and then do a Join by ',' to output your desired string.

– glls
7 hours ago





the "proxy" you get is immutable, you can however, create a new array based on the proxy and then do a Join by ',' to output your desired string.

– glls
7 hours ago










2 Answers
2






active

oldest

votes


















4














Just as addition to gNerbs answer:



if you really need or want a own component you can use the iterator directive which has a first / last item attribute



Like:



<template>
<template iterator:it=animals>
<c-child key=it.value.key animal=it.value.value is-last=it.last></c-child>
</template>
</template>


Full Example for your Reference: https://developer.salesforce.com/docs/component-library/tools/playground/qaI8LcEzj/1/edit



Link to Docs: https://developer.salesforce.com/docs/component-library/documentation/lwc/create_lists






share|improve this answer






























    2














    Build your string in Javascript and just display it to the page as opposed to using the for:each template.



    @track animalString;

    @wire(getAnimals)
    assignAnimals(animals)
    this.animals = animals;

    this.animalString = this.animals.join(', ');



    Then:



    <span>!animalString</span>


    Or what ever element/component you want to use to display your string.






    share|improve this answer




















    • 3





      this.animalString = this.animals.join(', ');

      – sfdcfox
      7 hours ago











    • Nice! But it only works if I actually need to output a string - which I indeed do in this case. But what if I wanted to, for example, create a child component in an iteration and pass "isLast" as a parameter to this component? I wonder if there is some more general solution. But your solution works for my case this time, thanks again :)

      – mmm321321
      7 hours ago












    • @Renji-xD I think your answer is probably exactly what OP is looking for. You should past it as a separate answer as I'm sure it will be accepted.

      – gNerb
      5 hours ago











    • ok i added it - just don`t wanted to hijack your answer which is still valid of course ;)

      – Renji-xD
      5 hours ago











    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "459"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f265515%2flwc-detect-last-element-in-foreach-iteration%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    Just as addition to gNerbs answer:



    if you really need or want a own component you can use the iterator directive which has a first / last item attribute



    Like:



    <template>
    <template iterator:it=animals>
    <c-child key=it.value.key animal=it.value.value is-last=it.last></c-child>
    </template>
    </template>


    Full Example for your Reference: https://developer.salesforce.com/docs/component-library/tools/playground/qaI8LcEzj/1/edit



    Link to Docs: https://developer.salesforce.com/docs/component-library/documentation/lwc/create_lists






    share|improve this answer



























      4














      Just as addition to gNerbs answer:



      if you really need or want a own component you can use the iterator directive which has a first / last item attribute



      Like:



      <template>
      <template iterator:it=animals>
      <c-child key=it.value.key animal=it.value.value is-last=it.last></c-child>
      </template>
      </template>


      Full Example for your Reference: https://developer.salesforce.com/docs/component-library/tools/playground/qaI8LcEzj/1/edit



      Link to Docs: https://developer.salesforce.com/docs/component-library/documentation/lwc/create_lists






      share|improve this answer

























        4












        4








        4







        Just as addition to gNerbs answer:



        if you really need or want a own component you can use the iterator directive which has a first / last item attribute



        Like:



        <template>
        <template iterator:it=animals>
        <c-child key=it.value.key animal=it.value.value is-last=it.last></c-child>
        </template>
        </template>


        Full Example for your Reference: https://developer.salesforce.com/docs/component-library/tools/playground/qaI8LcEzj/1/edit



        Link to Docs: https://developer.salesforce.com/docs/component-library/documentation/lwc/create_lists






        share|improve this answer













        Just as addition to gNerbs answer:



        if you really need or want a own component you can use the iterator directive which has a first / last item attribute



        Like:



        <template>
        <template iterator:it=animals>
        <c-child key=it.value.key animal=it.value.value is-last=it.last></c-child>
        </template>
        </template>


        Full Example for your Reference: https://developer.salesforce.com/docs/component-library/tools/playground/qaI8LcEzj/1/edit



        Link to Docs: https://developer.salesforce.com/docs/component-library/documentation/lwc/create_lists







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 5 hours ago









        Renji-xDRenji-xD

        30613




        30613























            2














            Build your string in Javascript and just display it to the page as opposed to using the for:each template.



            @track animalString;

            @wire(getAnimals)
            assignAnimals(animals)
            this.animals = animals;

            this.animalString = this.animals.join(', ');



            Then:



            <span>!animalString</span>


            Or what ever element/component you want to use to display your string.






            share|improve this answer




















            • 3





              this.animalString = this.animals.join(', ');

              – sfdcfox
              7 hours ago











            • Nice! But it only works if I actually need to output a string - which I indeed do in this case. But what if I wanted to, for example, create a child component in an iteration and pass "isLast" as a parameter to this component? I wonder if there is some more general solution. But your solution works for my case this time, thanks again :)

              – mmm321321
              7 hours ago












            • @Renji-xD I think your answer is probably exactly what OP is looking for. You should past it as a separate answer as I'm sure it will be accepted.

              – gNerb
              5 hours ago











            • ok i added it - just don`t wanted to hijack your answer which is still valid of course ;)

              – Renji-xD
              5 hours ago















            2














            Build your string in Javascript and just display it to the page as opposed to using the for:each template.



            @track animalString;

            @wire(getAnimals)
            assignAnimals(animals)
            this.animals = animals;

            this.animalString = this.animals.join(', ');



            Then:



            <span>!animalString</span>


            Or what ever element/component you want to use to display your string.






            share|improve this answer




















            • 3





              this.animalString = this.animals.join(', ');

              – sfdcfox
              7 hours ago











            • Nice! But it only works if I actually need to output a string - which I indeed do in this case. But what if I wanted to, for example, create a child component in an iteration and pass "isLast" as a parameter to this component? I wonder if there is some more general solution. But your solution works for my case this time, thanks again :)

              – mmm321321
              7 hours ago












            • @Renji-xD I think your answer is probably exactly what OP is looking for. You should past it as a separate answer as I'm sure it will be accepted.

              – gNerb
              5 hours ago











            • ok i added it - just don`t wanted to hijack your answer which is still valid of course ;)

              – Renji-xD
              5 hours ago













            2












            2








            2







            Build your string in Javascript and just display it to the page as opposed to using the for:each template.



            @track animalString;

            @wire(getAnimals)
            assignAnimals(animals)
            this.animals = animals;

            this.animalString = this.animals.join(', ');



            Then:



            <span>!animalString</span>


            Or what ever element/component you want to use to display your string.






            share|improve this answer















            Build your string in Javascript and just display it to the page as opposed to using the for:each template.



            @track animalString;

            @wire(getAnimals)
            assignAnimals(animals)
            this.animals = animals;

            this.animalString = this.animals.join(', ');



            Then:



            <span>!animalString</span>


            Or what ever element/component you want to use to display your string.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 5 hours ago

























            answered 8 hours ago









            gNerbgNerb

            6,421937




            6,421937







            • 3





              this.animalString = this.animals.join(', ');

              – sfdcfox
              7 hours ago











            • Nice! But it only works if I actually need to output a string - which I indeed do in this case. But what if I wanted to, for example, create a child component in an iteration and pass "isLast" as a parameter to this component? I wonder if there is some more general solution. But your solution works for my case this time, thanks again :)

              – mmm321321
              7 hours ago












            • @Renji-xD I think your answer is probably exactly what OP is looking for. You should past it as a separate answer as I'm sure it will be accepted.

              – gNerb
              5 hours ago











            • ok i added it - just don`t wanted to hijack your answer which is still valid of course ;)

              – Renji-xD
              5 hours ago












            • 3





              this.animalString = this.animals.join(', ');

              – sfdcfox
              7 hours ago











            • Nice! But it only works if I actually need to output a string - which I indeed do in this case. But what if I wanted to, for example, create a child component in an iteration and pass "isLast" as a parameter to this component? I wonder if there is some more general solution. But your solution works for my case this time, thanks again :)

              – mmm321321
              7 hours ago












            • @Renji-xD I think your answer is probably exactly what OP is looking for. You should past it as a separate answer as I'm sure it will be accepted.

              – gNerb
              5 hours ago











            • ok i added it - just don`t wanted to hijack your answer which is still valid of course ;)

              – Renji-xD
              5 hours ago







            3




            3





            this.animalString = this.animals.join(', ');

            – sfdcfox
            7 hours ago





            this.animalString = this.animals.join(', ');

            – sfdcfox
            7 hours ago













            Nice! But it only works if I actually need to output a string - which I indeed do in this case. But what if I wanted to, for example, create a child component in an iteration and pass "isLast" as a parameter to this component? I wonder if there is some more general solution. But your solution works for my case this time, thanks again :)

            – mmm321321
            7 hours ago






            Nice! But it only works if I actually need to output a string - which I indeed do in this case. But what if I wanted to, for example, create a child component in an iteration and pass "isLast" as a parameter to this component? I wonder if there is some more general solution. But your solution works for my case this time, thanks again :)

            – mmm321321
            7 hours ago














            @Renji-xD I think your answer is probably exactly what OP is looking for. You should past it as a separate answer as I'm sure it will be accepted.

            – gNerb
            5 hours ago





            @Renji-xD I think your answer is probably exactly what OP is looking for. You should past it as a separate answer as I'm sure it will be accepted.

            – gNerb
            5 hours ago













            ok i added it - just don`t wanted to hijack your answer which is still valid of course ;)

            – Renji-xD
            5 hours ago





            ok i added it - just don`t wanted to hijack your answer which is still valid of course ;)

            – Renji-xD
            5 hours ago

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Salesforce Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f265515%2flwc-detect-last-element-in-foreach-iteration%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

            Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

            199年 目錄 大件事 到箇年出世嗰人 到箇年死嗰人 節慶、風俗習慣 導覽選單