PricingBlog

How can I use a value as a key inside a repeating loop

  • rscott-1198902189981171772

    Bishop

    2 years ago

    I have a repeating loop going through a list of keys. topic, title, things like that.

    I have an object where 'topic', and 'title' are keys, and there's values for them

    How can extract the value for the keys in the repeating loop?

    The attached video explains it pretty clearly.

    I don't see how to provide the key so I can get that value out.
  • rscott-1199186972661526639

    Bishop

    2 years ago

    Thanks Andreas! This is the kind of engagement that will make Toddle successful.

    It's the GET function which makes a lot of sense.

    My other understanding about GET is that if a key doesn't exist, it will not error, but give null which is good.
  • rscott-1199218401348956252

    Bishop

    2 years ago

    Really helpful but I had to change the data structure a bit on xano and now i'm getting data that needs to go into an object and i can't seem to iterate on it.
  • lucasg-1199220271232589854

    Lucas G

    2 years ago

    Try using the map node instead. It iterates over all the objects in the array. Set is used to set an object but since you are only declaring one item, it is simply creating one object
  • *selecting one item from the API
  • rscott-1199235848189779988

    Bishop

    2 years ago

    Thanks Lucas. I'm clearly missing something in how to use map. I saw a tutorial somewhere but can't find it now. Because all the data ends up in another array I'm not sure its the thing. I might need a for-each. Or maybe I can assign to the object from within the formula for the map, I don't know.

    My goal:

    FROM: array
    [{"part":"title","answer":"The Hounds of London"},
    {"part":"author","answer":"John Adams"},
    ]

    TO: object:

    {
    "title" : "The Hounds of London",
    "author": "John Adams",
    "nextpart":"whatever"
    }
  • rscott-1199236503902113803

    Bishop

    2 years ago

    nope
    1199236503969206313-Screenshot_2024-01-23_at_12.17.30_AM.png
  • 767452073251438612-@Bishop
    nope
    lucasg-1199241581874643036

    Lucas G

    2 years ago

  • rscott-1199242080682262548

    Bishop

    2 years ago

    i want answers_obj to be this:

    {
    "title" : "The Hounds of London",
    "author": "John Adams",
    "nextpart":"whatever"
    }

    not item.
  • lucasg-1199242313537433700

    Lucas G

    2 years ago

    Can you paste me the API data real quick, I'll try to mock it up and send a screenshot
  • lucasg-1199242568593051740

    Lucas G

    2 years ago

    Actually, you don't need a Set node
  • Sorry, I'm tired lol
  • For the Map formula, set an object with the keys/values you want
  • lucasg-1199243140545138768

    Lucas G

    2 years ago

    1199243140310245406-image.png
  • rscott-1199243396854841384

    Bishop

    2 years ago

    you're making an array. i don't need an array i need a single object at the end.
  • lucasg-1199243522717515887

    Lucas G

    2 years ago

    Oh I thought the API was sending you an array
  • send book_id=4
  • rscott-1199244095739146321

    Bishop

    2 years ago

    it IS sending me an array but I want an object.

    Here's why I want an object: for every one of the textareas I'm displaying, I want to insert the answer they previously typed, and to look it up based on the partname (title, target, author) is fast.

    but the data in the database itself is stored in a format that gives me a list.

    The reason for that was when I tried to store it as an object, which I liked using, that was fine for read, but I was unable to update a single item in that object.

    Part of this is my lack of experience with xano.
  • lucasg-1199244149795344415

    Lucas G

    2 years ago

    Then can't you just use an Object node?
  • Instead of Set
  • rscott-1199244440397676605

    Bishop

    2 years ago

    i just don't know how to loop over the array and set pieces of the single object.

    Map can loop over an array, I understand that. But then how in the formula do i get it to extract the fields and put them into my answers_obj object
  • lucasg-1199244613693751316

    Lucas G

    2 years ago

    1199244613513379961-image.png
  • Wouldn't that work if you just need the one object?
  • Instead of the array, you'd select the actual value of course
  • rscott-1199244825933905990

    Bishop

    2 years ago

    Yeah except there's a ton of fields, it needs to be able to be defined.

    It works ok if I import it from an object, I think I just need to find out how to update that object on xano properly
  • Tod-1199244828614070273

    Tod

    2 years ago

    Great energy @Bishop! Your continuous contribution to the toddle Community just made you advance to Community Level 5!
  • lucasg-1199244842048425984

    Lucas G

    2 years ago

    I cannot access this link as is
  • Can you paste the data from the API?
  • [{"id":43,"created_at":1705984964395,"user_books_id":4,"part":"title","answer":"The Dogs O Thunda 2"},{"id":2,"created_at":1705559774236,"user_books_id":4,"part":"target","answer":"yourmom"},{"id":1,"created_at":1705559180909,"user_books_id":4,"part":"job","answer":"cows2"},{"id":23,"created_at":1705607270617,"user_books_id":4,"part":"blurb","answer":"cows coming home is a dumb book"},{"id":37,"created_at":1705610895464,"user_books_id":4,"part":"testman","answer":"cows3"}]
  • i am trying to get

    {part:answer,part:answer} where part is always different
  • there's going to be over 100 parts and the client needs to be able to change them so it gets a little tricky, cannot hardcode anything
  • lucasg-1199245786752487455

    Lucas G

    2 years ago

    1199245786144321608-image.png
  • Like that?
  • lucasg-1199245856923189368

    Lucas G

    2 years ago

    Ah I see
  • Gotcha
  • rscott-1199245922815709214

    Bishop

    2 years ago

    its correct but the keys canot be hardcoded
  • lucasg-1199245922983485481

    Lucas G

    2 years ago

    Ok
  • rscott-1199245928285093919

    Bishop

    2 years ago

    check this out
  • lucasg-1199245946547097651

    Lucas G

    2 years ago

    Yeah I understand now
  • rscott-1199245958723162162

    Bishop

    2 years ago

    1199245958253396030-Screenshot_2024-01-23_at_12.54.47_AM.png
    1199245958530211860-Screenshot_2024-01-23_at_12.53.30_AM.png
  • rscott-1199246400878297158

    Bishop

    2 years ago

    2 different data structures I tried. the more traditional one, without the json, is easier to update on the xano side. its the one i'm screwing around with right now, trying to get it into an efficient state for toddle.

    the 1st one, i would extract the json in one fell swoop and it was already an object for toddle and reading it was fast cause its a hash.

    but when i went to update a single item in XANO, it was just failing to do that. I was just zeroing in on an answer I think. I would prefer using that version honestly, toddle loves it and its simple to maintain. i just gotta get it to save right.
  • lucasg-1199246912382058609

    Lucas G

    2 years ago

    In that case, when saving it you'd need to save the entire object
  • Sounds easier to do that
  • The body in the patch request would have to contain the entire body
  • rscott-1199247487983177759

    Bishop

    2 years ago

    i can do that but its gonna be quite a bit of data. its doable though. its an entire book, could be hundreds of pages of text.


    that said, I don't think that's entirely correct, I think i can just send a partname and answer. because in xano you can load an entire object record, theoretically alter one of the key/value pairs and then save the value back.

    This is where i'm struggling
  • rscott-1199247865889955901

    Bishop

    2 years ago

    i can't seem to get it to merge with the existing data, it goes in the wrong place or overwrites the whole thing. clearly i'm missing something obvious
    1199247865646690345-Screenshot_2024-01-23_at_1.02.25_AM.png
  • rscott-1199248296292667392

    Bishop

    2 years ago

    oh shit i figured it out
  • 1199248321064210523-Screenshot_2024-01-23_at_1.04.25_AM.png
  • 1199248454480834570-Screenshot_2024-01-23_at_1.05.06_AM.png
  • lucasg-1199248545497219132

    Lucas G

    2 years ago

    what a beast
  • good shit! 😁 that approach seems a lot simpler
  • rscott-1199248783368785970

    Bishop

    2 years ago

    its much simpler now i can just use the json for the whole book with all its many fields that the client can change for different purposes
  • yeah its a ton simpler all around.
  • whew
  • lucasg-1199248918102429797

    Lucas G

    2 years ago

    great job man
  • rscott-1199248924918165504

    Bishop

    2 years ago

    thanks for strugglig through it with me, you said something that triggered me to get this right finally
    🔥1
  • lucasg-1199248926042247168

    Lucas G

    2 years ago

    it's coming along
  • rscott-1199249276727996496

    Bishop

    2 years ago

    Yeah, my thing is ugly AF but i have a designer and design, just getting the functinoality done and this was pretty much the last major thing - saving and loading the answers. Super responsive. First version was on wordpress with javascript - bad news. Very excited to have found toddler.

    I just don't like telling people that I'm searching for more toddlers to help out around the office. They think that's weird.
  • lucasg-1199249497881071647

    Lucas G

    2 years ago

    lmao that'll put you in some type of list I'm sure
  • thankfully the community has a lot of good people
  • feel free to reach out if anything as well
  • rscott-1199249700826656808

    Bishop

    2 years ago

    thanks brother
    🙏1