0

I have an array of items called documents. Each item has the same bunch of properties. I wanted to add an additional property to each item called serviceShortCode. I wrote the following code to achieve this

documents = _.map(documents, function (doc) {
                return _.extend({                    
                    serviceShortCode: View.getServiceName(doc.publishedIn)      
                });
            });

Once this code finishes executing I get an array of objects with only the serviceShortCode, all other properties have vanished.

Why so?

1 Answer 1

1

You seem to be missing the document from the extend call.

Change

return _.extend({                    
    serviceShortCode: View.getServiceName(doc.publishedIn)
});

To

return _.extend(doc, {                    
    serviceShortCode: View.getServiceName(doc.publishedIn)
});

Or, alternatively

return _.extend({}, doc, {                    
    serviceShortCode: View.getServiceName(doc.publishedIn)
});

to ensure the original objects remain unmodified.

Sign up to request clarification or add additional context in comments.

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.