Forum Discussion
Good day, you can also use the slice() function of an array… see sample below where $object have 210 key-value pair and $array has a length of 210
For grouping key-value pair by the 100s
‘,’.repeat(Math.floor($object.keys().length / 100)).split(‘,’).map((x,i)=> $object.filter((v,k)=> $object.keys().slice((i * 100), (i + 1) * 100).indexOf(k) != -1))
For grouping array by 100s
‘,’.repeat(Math.floor($array.length / 100)).split(‘,’).map((x,i)=> $array.slice((i * 100), (i + 1) * 100))
~Alchemiz
Thanks Alchemiz , the array is now spliited in two. One array with 100 elements and the other with the remaining elements.
This is the current output :
"ShipmentCreateDate": "01/01/2023",
"ShipmentID": "1",
"SerialNumbers": [
[
{
"SerialNo": "123"
},
{ "SerialNo":"456"
}
],
[
{
"SerialNo": "101"
},
{
"SerialNo": "102"
}
]
]
I was wondering if something like this output is possible :
"ShipmentCreateDate": "01/01/2023",
"ShipmentID": "1",
"SerialNumbers": [
[
{
"SerialNo": "123"
},
{
"SerialNo":"456"
}
]
"ShipmentCreateDate": "01/01/2023",
"ShipmentID": "1",
"SerialNumbers": [
[
{
"SerialNo": "101"
},
{
"SerialNo": "102"
}
]
]
Where the first output would be the array with the 100 elements and the remaining fields that are not part of the array and the second output would be the array with the remaining elements and the fields that are not part of the array.
Regards,
Igor
- alex_panganiban7 years agoContributor
Hi CJ, your solution has been really helpful to me. Thank you so much. I have an add-on question about this solution though. As it’s building the new array using the reduce statement you passed on to me, how can I insure that I’m not putting duplicate values into it? Hoping you can help.
Thanks, Alex
- cjhoward187 years agoEmployee
glad it was helpful.
You can use a map to store your values instead of an array and store the values as your keys. Maps do not allow for duplicate key entries, which would take care of this problem for you, and you could collect your ‘values array’ by using keys() method described here:
https://docs-snaplogic.atlassian.net/wiki/spaces/SD/pages/1439367/Object+Functions+and+Properties#ObjectFunctionsandProperties-keysThis is just one solution, there are many ways you can keep track of this if needed.