Merging arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Athensmusic
    Member
    • Feb 2020
    • 51

    Merging arrays

    **Already Solved using 'while', I thought it could be done by a shorter formula.

    Good mornings,
    I've been attempting to combine two arrays without success.
    Here's the scenario:
    In an entity I have two related as Many relationships called 'first' and 'second', and an array field named 'all'. My goal is for 'all' to contain both firstIds and secondIds. In my example, firstIds consists of four related records, and secondIds consists of two. So ideally, 'all' field should contain six objects.
    However, every attempt I've made so far has resulted in only five objects (four objects from firstIds and the other two IDs grouped as one object).
    Seems I've tried everything except the correct formula
    Any suggestions on how to achieve the desired result?
    Thanks​
    Last edited by Athensmusic; 04-27-2024, 07:08 AM.
  • lazovic
    Super Moderator
    • Jan 2022
    • 810

    #2
    Hi Athensmusic,

    Please tell me have you tried using the following formula script?
    Code:
    all = array\push(all, firstIds);
    all = array\push(all, secondIds);

    Comment

    • Athensmusic
      Member
      • Feb 2020
      • 51

      #3
      Hi lazovic
      Yes, I tried but this makes one object for firstsIds and one for secondIds but I needed all objects separeted.
      I did that using while, didn't figure out any shorter code.
      See the difference in pics.
      Click image for larger version

Name:	2.jpg
Views:	170
Size:	18.5 KB
ID:	105616 Click image for larger version

Name:	1.jpg
Views:	145
Size:	15.7 KB
ID:	105618
      Attached Files

      Comment

      • lazovic
        Super Moderator
        • Jan 2022
        • 810

        #4
        Hi Athensmusic,

        Thank you for your clarifications, I understood you and was able to reproduce this case. I think this is how everything should work, since, for example, firstIds field is a full-fledged array and when using an array\push() function this array is not decomposed into individual elements.

        Please tell me do I understand correctly that your formula script looks like this?
        Code:
        $firstCount = array\length(firstIds);
        $secondCount = array\length(secondIds);
        
        $i = 0;
        
        while ($i < $firstCount) {
        
          all = array\push(all, array\at(firstIds, $i));
        
          $i = $i + 1;
        }
        
        $y = 0;
        
        while ($y < $secondCount) {
        
          all = array\push(all, array\at(secondIds, $y));
        
          $y = $y + 1;
        }

        Comment

        • Athensmusic
          Member
          • Feb 2020
          • 51

          #5
          Hi there lazovic
          I didn't tested your way but I quote my working version.
          Actually I needed to get the results in a variable ($all) so to pass it later in another formula and not straight in an array field.
          So it is a part of the larger formula, but works if modified for an array field too.


          Code:
          $i=0;
          $all=firstIds;
          while ($i < array\length(secondIds),
          $secondId = array\at(secondIds, $i);
          $all = array\push($all, $secondId);
          $i = $i + 1;
          );​​
          Last edited by Athensmusic; 05-02-2024, 06:57 AM.

          Comment

          Working...