cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

To add elements in an array object

arvindnsn
Contributor

My requirement is based on the input parameter(no of days) of a pipeline, I have to execute the the child pipeline which calls a API URL like below.

If the input paramater is 4, the child pipleline should execute as below.
https://xxxx/xxxx/testing?startdate=10-17-2018&enddate=10-17-2018
https://xxxx/xxxx/testing?startdate=10-17-2018&enddate=10-16-2018
https://xxxx/xxxx/testing?startdate=10-17-2018&enddate=10-15-2018
https://xxxx/xxxx/testing?startdate=10-17-2018&enddate=10-14-2018.

To accomplish this, i am trying to build an array with dates dynamically based on the input paramater. If the input paramater is 4 the my array variable should be like [โ€œ10-17-2018โ€, โ€œ10-16-2018โ€, โ€œ10-15-2018โ€, โ€œ10-14-2018โ€] and then pass these dates as documents to the child pipeline calling the API.

Is this the correct approach and if so how do i build the array dynamically (ie some sort of looping is required i believe which i am not able to achieve in SnapLoigc). I would really appreciate if a solution is provided.

Thanks

1 ACCEPTED SOLUTION

tstack
Former Employee

You can use the Sequence snap at the head of the pipeline to generate a series of documents with an index number that you can then transform with a Mapper to get a series of dates.

If you canโ€™t make the Sequence snap the head of the pipeline, you can use the sl.range() function to generate an array and then use the Array.map() method to transform the dates.

sl.range(_days).map(x => Date.now().minusDays(x).toLocaleDateString({ format: "MM-dd-yyyy" }))

To break this down:

  • sl.range(_days) - The _days pipeline parameter contains the number of days to go back. The sl.range(...) call then produces an array that looks like [0, 1, 2, 3].
  • .map(x => ...) - Each element in the array will be transformed by the given callback function.
  • Date.now().minusDays(x).toLocaleDateString({ format: "MM-dd-yyyy" }) - This will get the current date-time, subtract one day and then turn the result into a string.

Iโ€™m attaching a pipeline that demonstrates both methods:

DateArray_2018_10_17.slp (6.9 KB)

View solution in original post

2 REPLIES 2

tstack
Former Employee

You can use the Sequence snap at the head of the pipeline to generate a series of documents with an index number that you can then transform with a Mapper to get a series of dates.

If you canโ€™t make the Sequence snap the head of the pipeline, you can use the sl.range() function to generate an array and then use the Array.map() method to transform the dates.

sl.range(_days).map(x => Date.now().minusDays(x).toLocaleDateString({ format: "MM-dd-yyyy" }))

To break this down:

  • sl.range(_days) - The _days pipeline parameter contains the number of days to go back. The sl.range(...) call then produces an array that looks like [0, 1, 2, 3].
  • .map(x => ...) - Each element in the array will be transformed by the given callback function.
  • Date.now().minusDays(x).toLocaleDateString({ format: "MM-dd-yyyy" }) - This will get the current date-time, subtract one day and then turn the result into a string.

Iโ€™m attaching a pipeline that demonstrates both methods:

DateArray_2018_10_17.slp (6.9 KB)

This is Brilliant having multiple solutions. Thanks so much for teaching to use the map method.