if I have a list:

Fruit apple text = a red fruit pear text = a green fruit

What’s an effective way to join the ‘text’ properties to create something like “a red fruit, a green fruit”.

Does this require a custom function or can the perchance list methods handle this?

thanks

  • VioneT@lemmy.worldM
    link
    fedilink
    English
    arrow-up
    3
    ·
    9 months ago

    You can use JavaScript array methods for that.

    Fruit
      apple
        text = a red fruit
      pear
        text = a green fruit
    
    output
      [Fruit.selectAll.map(fruit => fruit.text).join(", ")]
    
    • Fruit.selectAll will create an array of all the items in the Fruit list, you can also use .selectMany(...) or .selectUnique(...) to create an array of items.
    • .map(fruit => fruit.text) will essentially transform the array to have the text property of the fruit as the elements, instead of the fruit themselves.
    • .join(", ") would join the items into a single string with , as the delimiter