Joining properties of list items

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

4 points · 2 comments · view on lemmy.world

2 Comments

VioneT@lemmy.world · 3 pts · 1y (1 reply)

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
danMiller@lemmy.world · 1 pts · 1y

That works perfectly. Thanks so much for the explanation.