Forum Discussion

arvindnsn's avatar
arvindnsn
Contributor
7 years ago
Solved

To add elements in an array object

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 piplelin...
  • tstack's avatar
    7 years ago

    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)