cancel
Showing results for 
Search instead for 
Did you mean: 

Separate array values by index number into objects of matching index numbers

travis
New Contributor

I’m using a REST Get to Rest Post snap and I need to figure out how to separate the values of an array, by index number, and group each value into objects that contain all of the matching index numbers:

Current array formatting

“groceryStore”: {
“name”: “foodland”, “fruits”: {
“appleArray”: [“apple1”, “apple2”, “apple3”],
“orangeArray”:[“orange1”, “orange2”, “orange3”],
“bananaArray”:[banana1”, “banana2”, “banana3”],
},
}

Required array formatting

“groceryStore”: {
“name”: foodland”, “fruits”: [
{
“appleArray”: “apple1”,
“orangeArray”: “orange1”,
“bananaArray”: “banana1”,
}
{
“appleArray”: “apple2”,
“orangeArray”: “orange2”,
“bananaArray”: “banana2”,
}
{
“appleArray”: “apple3”,
“orangeArray”: “orange3”,
“bananaArray”: “banana3”,
}
]
}

2 REPLIES 2

tstack
Former Employee

You can use the sl.zip() method to join the arrays together by index. The result will be an array of 3-element arrays, so then a map() will be needed to convert the triples into objects.

sl.zip($groceryStore.fruits.appleArray,
       $groceryStore.fruits.orangeArray,
       $groceryStore.fruits.bananaArray)
  .map(x => { appleArray: x[0], orangeArray: x[1], bananaArray: x[2] })

travis
New Contributor

Yep! That worked wonderfully. Thank you!

Actual code:

sl.zip((jsonPath($, “$translations[].locale")), (jsonPath($, "$translations[].body”)), (jsonPath($, “$translations[*].title”))).map(x => {locale: x[0], body: x[1], title: x[2] })