How to Separate Results in Node Steps and Extract Specific Values?

This topic was automatically generated from Slack. You can find the original thread here.

Can someone tell me really quick how to seperate results in node steps? Let’s say I pass a step, and the results is “number: 100”, but I only want to use the 100 and not the “number:” part, how do I extract that?

Hi , I think your question is a great case for Pipedream bot to help. I’ll pass your question to the Pipedream bot now. Please feel free to ask it any question

Can someone tell me really quick how to seperate results in node steps? Let’s say I pass a step, and the results is “number: 100”, but I only want to use the 100 and not the “number:” part, how do I extract that?

You can extract the number from the string using JavaScript in a Node.js code step. Here’s an example of how to do that: ```
export default defineComponent({ async run({ steps }) { const inputString = steps.previousStep.result; // Replace ‘previousStep’ with the actual name of the step that returns the “number: 100” string const number = parseInt(inputString.split(‘:’)[1].trim(), 10); return number; }, });

Awesome! Thank you. I will definitely be using this new bot in the future :heart_eyes:

I get the error “inputString.split is not a function”

Apologies for the confusion. It seems like the inputString might not be a string. To ensure it’s a string before splitting, you can convert it to a string using the toString() method. Here’s the updated code: export default defineComponent({ async run({ steps }) { const inputString = steps.previousStep.result; // Replace 'previousStep' with the actual name of the step that returns the "number: 100" string const number = parseInt(inputString.toString().split(':')[1].trim(), 10); return number; }, }); This code first converts the inputString to a string using toString(), then splits it by the colon, takes the second part, trims any whitespace, and parses it as an integer. The extracted number is then returned from the step.

Now i get the error “Cannot read property ‘trim’ of undefined”

I apologize for the confusion earlier. It seems that the split operation might not be working as expected. Let’s add a check to ensure the split operation is successful before attempting to trim and parse the number. Here’s the updated code: export default defineComponent({ async run({ steps }) { const inputString = steps.previousStep.result; // Replace 'previousStep' with the actual name of the step that returns the "number: 100" string const parts = inputString.toString().split(':'); if (parts.length >= 2) { const number = parseInt(parts[1].trim(), 10); return number; } else { throw new Error('Failed to extract the number from the input string'); } }, }); This code first converts the inputString to a string using toString(), then splits it by the colon. It checks if there are at least two parts after splitting, and if so, it trims any whitespace from the second part and parses it as an integer. The extracted number is then returned from the step. If the split operation doesn’t result in at least two parts, an error is thrown.

I need to first convert json into string, please give me the code to do so

To convert a JSON object into a string, you can use the JSON.stringify() method in JavaScript. Here’s an example: export default defineComponent({ async run({ steps }) { const jsonObject = { key: "value", number: 100 }; const jsonString = JSON.stringify(jsonObject); return jsonString; }, }); This code creates a JSON object, converts it into a string using JSON.stringify(), and then returns the JSON string from the step.

how do i format a large number to seperate it with commas? for example: 1000000 would be 1,000,000

To format a large number with commas as separators in JavaScript, you can use the toLocaleString() method. Here’s an example: const number = 1000000; const formattedNumber = number.toLocaleString(); console.log(formattedNumber); // Output: "1,000,000" This code takes the number 1000000 and formats it with commas as separators using the toLocaleString() method. The formatted number is then logged to the console as “1,000,000”.

I want to do this for all large numbers, and I want it to format them automatically.

To format all large numbers in an array with commas as separators, you can use the toLocaleString() method in a loop. Here’s an example: const numbers = [1000000, 500000, 2500000]; const formattedNumbers = numbers.map(number => number.toLocaleString()); console.log(formattedNumbers); // Output: ["1,000,000", "500,000", "2,500,000"] This code takes an array of large numbers, formats each number with commas as separators using the toLocaleString() method, and stores the formatted numbers in a new array.

I want to format all numbers over 1000 to include decimals in their rightful places

To format all numbers over 1000 in an array to include decimals in their rightful places, you can use the toLocaleString() method with specific options. Here’s an example: const numbers = [1000, 1500.5, 2500.25, 999, 5000.123]; const formattedNumbers = numbers.map(number => { if (number > 1000) { return number.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2, }); } return number; }); console.log(formattedNumbers); // Output: [ '1,000.00', '1,500.50', '2,500.25', 999, '5,000.12' ] This code takes an array of numbers and formats each number with commas as separators and decimals in their rightful places using the toLocaleString() method, but only if the number is greater than 1000. The formatted numbers are then stored in a new array.

how do I respond to an email that was sent to my unique pipedream email that is used for the trigger?

To respond to an email sent to your unique Pipedream email used for the trigger, you can use the “Send Yourself an Email” action in a Pipedream workflow. Here’s an example of how to set up a workflow to respond to an email: 1. Create a new workflow with the “New Email” trigger from the Email app. This will trigger your workflow when a new email is sent to your unique Pipedream email address. 2. Add the “Send Yourself an Email” action from the Email app to your workflow. Customize the subject and text (or HTML) content of the email as needed. Here’s an example of a Pipedream component that sends an email to yourself: ```
import { axios } from “@pipedream/platform”; export default defineComponent({ async run({ steps, $ }) { const options = { subject: ‘Your subject here’, text: ‘Your text here’ }; $.send.email(options); }, });