cancel
Showing results for 
Search instead for 
Did you mean: 

How can I pop the last element from one array and append it to another?

adam_gataev
New Contributor II

Hello,

I’m working with this JSON structure:

[
  {
    "field_a": "string",
    "entries": [{}, {}, {}],
    "shared": []
  }
]

I need to remove only the last object from the entries array and append it to the shared array, leaving the other entries intact. What’s the most straightforward way to accomplish this?

Thank you for your help.

Kind regards,
Adam

2 ACCEPTED SOLUTIONS

koryknick
Employee
Employee

@adam_gataev - you have the right terminology.  Just use a Mapper as follows:

koryknick_0-1747828109681.png

Here I'm just using two Array methods: concat() and pop() to complete your goal in one expression.  The Array.pop() removes the last element and returns that element.  The Array.concat() creates a new array with the new element added in.

Note that I'm also using the "pass-through" option on the Mapper settings.  This allows any other elements in the input doc to flow through to the target path without specifying them.

Hope this helps!

 

View solution in original post

You're the man @koryknick, thank you so much!

View solution in original post

2 REPLIES 2

koryknick
Employee
Employee

@adam_gataev - you have the right terminology.  Just use a Mapper as follows:

koryknick_0-1747828109681.png

Here I'm just using two Array methods: concat() and pop() to complete your goal in one expression.  The Array.pop() removes the last element and returns that element.  The Array.concat() creates a new array with the new element added in.

Note that I'm also using the "pass-through" option on the Mapper settings.  This allows any other elements in the input doc to flow through to the target path without specifying them.

Hope this helps!

 

You're the man @koryknick, thank you so much!