0

Not Exit from a loop

Hi to all!

 

i have got this loop on a formula:

 

let images := response.result.images;
let coverimageLink := "";
let foundImage := false;
for i in images do
    if (i.type = "primary" or i.type = "secondary") and not foundImage then
        let coverimageLink := i.resource_url;
        coverimage := coverimageLink;
        foundImage = true
    end
end

 

Copy

 

it extract data from a Json like this:

 

 i am trying to extract the first resource_url found independently if "primary" or "secondary" (but it has to be the first), but for some reason I can not understand the formula doesn't break the loop at the first resource_url found and the output got is the last:

 

https://i.discogs.com/CYFRLcNeKyPFEn1Tv_MFDNOy0rStKVHxDSl5SKx09LE/rs:fit/g:sm/q:90/h:591/w:600/czM6Ly9kaXNjb2dz/LWRhdGFiYXNlLWlt/YWdlcy9SLTE1OTY1/MDAtMTQ2NTU3OTk2/MC0xNzIxLmpwZWc.jpeg

3 replies

null
    • Sean
    • 1 yr ago
    • Reported - view

    If you only need the first item in the array you should just use the item() function.

    item(images, 0).resource_url

    The only for-loop style you can break out of is this one and you do it by changing the value of i.

    • francescostefanello
    • 1 yr ago
    • Reported - view

    i had to "reParse" the JSON to use the item function.

    And this:

     

    let images := text(response.result.images);
    let images := parseJSON(images);
    let coverimageLink := item(images, 0).resource_url;
    coverimage := coverimageLink

     

    works in an excellent way

     

    THANK YOU a lot Sean

      • Sean
      • 1 yr ago
      • Reported - view

       

      You might be able to skip the first 2 lines by using this...

      let coverimageLink := item(response.result, 0).resource_url

      It's similar to this thread.

Content aside

  • Status Answered
  • 1 yr agoLast active
  • 3Replies
  • 109Views
  • 2 Following