0

This is the basic JavaScript for loop I'm trying to replace:

for (var i=0; i<tickers.length; i++) {
    if (tickers[i].ticker === selectedTicker) {
        selectTicker('portfolio', tickers[i]);
        break;
    }
}

Here is the lodash version

_.times((tickers.length), function() {
    if (tickers[i].ticker === selectedTicker) {
        selectTicker('portfolio', tickers[i]);
        return;
    }
});

Obviously this errors out currently, because there is no [i] variable set in the lodash version.

The lodash version is much more readable imho, just do this many times the length of my tickers array.

However I need to compare the ticker of each object to the selectedTicker.


UPDATE: Adding screenshot and link to prove to some asking that _lodash is faster than native Javascript.

http://jsperf.com/forloop-vs-each-from-lodash

enter image description here

enter image description here

1
  • Can you also add the jsperf comparison of _.find and _.filter/Native .filter? Commented Feb 17, 2016 at 3:56

3 Answers 3

2

You can use _.find()

var ticker = _.find(tickers, {'ticker': selectedTicker});

// `find()` returns an object if the element is found in array
// it returns `undefined` if not found
if (ticker) {
    // If element found in the array call the function
    selectTicker('portfolio', ticker);

    // return; // To return from the function
}
Sign up to request clarification or add additional context in comments.

5 Comments

Ah thanks! Nice! This found it, however now I need to tell the selectTicker function which item in tickers to pass in. How would you accomplish that here?
@LeonGaban The find method returns object or undefined. You can use if (foundResult) { selectTicker(foundResult.key) }. In case, to send more than one elements from object, you can use _.pick() or _.omit
Thanks again, I'm trying that now, however it has to loop through all items in my Array before finishing, I want it to stop as soon as the foundResult has been found and the next function runs. However foundResult always shows true, and the return statement doesn't break the loop in this case.
@LeonGaban You don't need the loop to get the matching object from array, just use _.find. Just use return after function call.
Ok sorry I misunderstood, I understand now thanks for the explanation. +1
1

You need to pass i into the callback in the lodash function. This should work:

_.times((tickers.length), function(i) {
    if (tickers[i].ticker === selectedTicker) {
      selectTicker('portfolio', tickers[i]);
      return;
    }
});

Also, if you're able to use es6 syntax, you can achieve the same result by using filter

var ticker = tickers.filter(t => t.ticker === selectedTicker)[0];
if(ticker){
  selectTicker('portfolio', ticker);
}

Comments

1

You have to add an argument to the function:

_.times((tickers.length), function( i ) {
    if (tickers[i].ticker === selectedTicker) {
        selectTicker('portfolio', tickers[i]);
        return;
    }
});

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.