Forum Discussion

srikotla's avatar
srikotla
New Contributor
7 years ago

How to increment the counter (variable) based on the incoming data in snaplogic

How to increment the counter (variable) based on the incoming data in snaplogic.

Source Data:

CostCenter, TestCompany
Test_Cost_Center_001,Test_company_1
Test_Cost_Center_001,Test_company_2
Test_Cost_Center_002,Test_company_1
Test_Cost_Center_002,Test_company_2
Test_Cost_Center_002,Test_company_3
Test_Cost_Center_002,Test_company_4

Target Data should be:

CostCenter, TestCompany, ID
Test_Cost_Center_001,Test_company_1, 1
Test_Cost_Center_001,Test_company_2, 2
Test_Cost_Center_002,Test_company_1, 1
Test_Cost_Center_002,Test_company_2, 2
Test_Cost_Center_002,Test_company_3, 3
Test_Cost_Center_002,Test_company_4, 4

Experts over here, please help in achieving the above solution using the right snap.

7 Replies

  • tstack's avatar
    tstack
    Former Employee

    I think you can use the .map() method on Array objects to do this since the callback will be passed the current index into the array. The following expression does this:

    $lines.map((elem, index) => { lineNumber: index + 1, ...elem })
    

    The .map() method will iterate over each element in the $lines array and execute the given callback function (the part that starts with (elem, index) =>. The callback function creates a new object with the new lineNumber property and then uses the spread operator (...elem) to add in the properties from the original array element.

    Here’s a pipeline that demonstrates this:

    Community7085_2020_03_16.slp (3.6 KB)

    • wcl3y2's avatar
      wcl3y2
      New Contributor III

      That did the trick. Thank you!