0

I'm trying to loop through a pipe delimited list passed to a function, split it out into an array based on the pipe as a separator, and then break each item out into its component parts, where the format is as follows:

"76:1167|76:1168"

so that the array would be: surveyQuestions[0] = 76:1167. And then that would be split up into : surveyQuestions[0].question = 76 and surveyQuestions[0].answer = 1167.

And this is the code I'm using, but the values come back undefined when I try to add the properties to each array item.

function answerSurvey(survey){

var surveyResults = survey.split("|");

for (var i=0;i<surveyResults.length;i++){

    var surveyResult = surveyResults[i].split(":");
    var surveyQ = surveyResult[0];
    var surveyA = surveyResult[1];
    surveyResults[i].surveyQ = surveyQ;
    surveyResults[i].surveyA = surveyA;
    console.log(surveyResults[i].surveyQ + "|" + surveyResults[i].surveyA)
}

}

answerSurvey("76:1167|76:1168");

3 Answers 3

3

You are trying to add a property to a string, which you cannot do. If you want the Array to contain a list of Objects, use Array.map() to transform your strings into objects:

var surveyResults = survey.split("|").map(function (result) {
    var parts = result.split(":");
    return {
        question: parts[0],
        answer: parts[1]
    };
});

It is included in most browsers, but for older versions of some browsers, you'll need to add .map() manually.

Edit: jQuery does add a map function (as noted in the comments). Tweak the code above slightly to include the array as the first parameter to $.map() and substitute the argument name for this (or shift the result argument one to the right, following index):

var surveyResults = $.map(survey.split("|"), function (i, result) {
    var parts = result.split(":");  // or this.split(":")
    return {
        question: parts[0],
        answer: parts[1]
    };
});
Sign up to request clarification or add additional context in comments.

3 Comments

Also, the asker seems to be using jQuery, which offers a cross-browser implementation: $.map(array,function)
Thanks Jens - I'll probably do that
Good observation! I missed that.
1

Try this:

function answerSurvey(survey){

var surveyResults = survey.split("|");

for (var i=0;i<surveyResults.length;i++){

    var surveyResult = surveyResults[i].split(":");
    var surveyQ = surveyResult[0];
    var surveyA = surveyResult[1];
    surveyResults[i] = {};
    surveyResults[i].surveyQ = surveyQ;
    surveyResults[i].surveyA = surveyA;
    console.log(surveyResults[i].surveyQ + "|" + surveyResults[i].surveyA)
}

}

answerSurvey('76:1167|76:1168');

1 Comment

Corret. To point out the only difference to the OP, it's declaring surveyResults[i] to be an object ({}). Also, the argument for answerSurvey needs quotes around it.
0

surveyResults[i] is a 'string' not an object, so you can't add properties to it.

Try this:

var surveyQ = surveyResult[0];
var surveyA = surveyResult[1];
surveyResults[i] = {};
surveyResults[i].surveyQ = surveyQ;
surveyResults[i].surveyA = surveyA;

Example: http://jsfiddle.net/Paulpro/WeJxe/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.