1
var tickers = [];

for (var i=0; i<reportsArray.length; i++) {
    tickers.push(reportsArray[i].ticker);
}

Is there a way to replicate the above using the fastest / most efficient way in lodash.

This is what the objects may look like in the reportsArray:

{
    added_epoch: 1452873186
    details: ""
    reason: ""
    term: "Google rocks!"
    ticker: "GOOG"
    user_id: 1346753
    username: "leon"
}
2
  • Nothing in lodash is going to be faster than what you're doing but have you looked into use map? There's also a native implementation. Commented Feb 1, 2016 at 20:58
  • I agree with @MikeC, but you can verify by creating tests with realistic data on jsperf.com Commented Feb 1, 2016 at 21:01

2 Answers 2

6

Array.prototype.map() is all you need.

var tickers = reportsArray.map(function (report) {
    return report.ticker;
});

And, if you want to do this in Lodash anyway, use _.map() to achieve the same result.

var tickers = _.map(reportsArray, function (report) {
    return report.ticker;
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I renamed my title to better fit, also great answer.
1

You can use just

var tickers = _.map(reportsArray, 'ticker');

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.