Parse collection output into an array

#1

I’m using the Code service and trying to parse the collection output into an array. I’ve been using the ‘&&’ modifier to turn it into a list, but I’ve come to realize that the entire output is still a string. What could I do to achieve this result?

Code snippet:
var answeredQuestions = inputs.answeredQuestions;

Inputs
Inputs.answerQuestions = {{look_for_matching_response_id.results_+_gsx:question &&}}

Collection output
“answeredQuestions”: “870, 870, 870, 870, 870, 870, 870, 870, 870, 870 & 870”,

(Karen Barker) #2

Hi @inviz,

If you use the * modifier rather than && in your input I think this should achieve what you’re looking for :slight_smile:

#3

@KarenBarker - thanks for the question response. Your suggestion only got me 1/3 of the way. Mainly because the output is still a string but with new line returns. So, here’s what I ended up doing to achieve the desired result.

// pulled in raw string values
var answeredQuestions = inputs.answeredQuestions;

// replaced all new line returns with commas
answeredQuestions = answeredQuestions.replace(/\n/g, “,”);

// converted the comma-delimited string to array
answeredQuestions = answeredQuestions.split(’,’);

(Karen Barker) #4

Hi,

Yes sorry I missed that section - you would actually be able to create this into an array in 1 line as follows:

answeredQuestions = inputs.answeredQuestions.split("\n"); :slight_smile:

#5

Thank you! Much more efficient.

1 Like