Replace All, Loop?

  • kawwl-1333079205151244338

    Kawwl

    13 days ago

    Hi,

    I'm playing with OpenAI Assistants API.
    I have read we are not doing loops in Toddle, but I can't get the logic of how to solve this case.

    What I don't understand
    I need to iterate over, or "know" how to replace all instances in a string, with new data, based on another array.

    I have a string:
    Hello [text_1] this is a string. [text_2]

    I have an array (simplified example):
    [
    {
    "text": "[text_1]"
    },
    {
    "text": "[text_2]"
    }

    I want to replace [text_1] with "[0]" and [text_2] with "[1]". (Use index for now, maybe something else later)

    Desired result:
    Hello [0] this is a string. [1]

    Any ideas?
    1333079205382197258-image.png
  • gazinhio-1333381667531788404

    gazinhio

    12 days ago

    Try using Split rather than Replace All and have the [4:3....] text as the delimiter. This will generate an array of the text with each section broken down into an element at each instance of [4.3...]. You can then map it to generate an array with the text and the index.
  • kawwl-1333401836073390103

    Kawwl

    12 days ago

    The numbers /string inside the [] does vary; so I will not have a fixed delimiter. If we had loop, I would think it could look like this:

    For each item in array ie. (item = [text_1]),
    Find and replace [text_1] in STRING, with item.index

    This is just for the initial testing I think. In the end i will probably do this in the backend, but right now I'm using API/response for MVP.

Stop scrolling. Start building.

toddle is a visual web app builder that rivals custom code — but accessible to your entire team!

Try toddle — it's free!

© Copyright 2024 toddle. All rights reserved.