Question

I have this code:

        var answers = _.clone($scope.question.answers)
        var answers = {};
        $scope.question.answers.forEach(function (element, index) {
            answers[index].answerUid = element.answerUid;
            answers[index].response = element.response;
        });

Is there some way that I could simplify this using lodash ?

Was it helpful?

Solution

It is unclear to my what it is you are trying to iterate over and what it is you expect to have at the end. For instance, the way the code in the question is currently written, this line will cause an error:

answers[index].answerUid = element.answerUid;

because it will read answers[index] from the answers object, get undefined and try to access the field answerUid of the undefined value.

At any rate, I can cover the major cases. If you want answers to be an array, then this would do it:

var answers = _.map($scope.question.answers,
                    _.partialRight(_.pick, "answerUid", "response"));

This works whether $scope.question.answers is an array or an Object. The _.partialRight(_.pick, "answerUid", "response")) call is equivalent to:

function (x) {
    return _.pick(x, ["answerUid", "response"]);
}

The _.pick function picks the two fields answerUid and response out of the object.

If $scope.question.answers is a key/value mapping and you want a correponding mapping in answers, then this would do it:

var answers = _.mapValues($scope.question.answers,
                          _.partialRight(_.pick, "answerUid", "response"));

All the solutions here have been tested but it is not impossible that I introduced a typo in transcription.

OTHER TIPS

No need to use lodash, native reduce works just as well - assuming that answers is a straight up array (looks like you are using angular?).

 var answers = _.clone($scope.question.answers)
 var filteredAnswers = answers.reduce(function(working, current, index ){
     working.push({
        answerUid: current.answerUid,
        response: current.response
     });
     return working;
 },[]);

If that's confusing, here's another example of using reduce to sum an array of numbers:

[1,2,3].reduce(function(sum, current){return sum + current;},0); // 6

The zero at the end is the initial value passed into the callback (as sum above), and the value you return is passed on to the next call of the function. Keep returning, filter inside your function body, and away you go. Reduce can return whatever structure you like, not just single primitive values.

edit: it looked like you were assigning keys on an object with numeric values - if you need an object as opposed to an array, then that would be a bit different.

You also need to wrap the function inside unary (docs). Otherwise you pass the key and the collection from iteratee to the pick function (docs) and it can (must not) lead to unexpected results.

var answers = _.mapValues($scope.question.answers, _.unary(_.partialRight(_.pick, 'answerUid', 'response')));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top