JavaScript Call function only once after a map() call function only once javascript,call function only once react,

I am having an issue on Nodejs, I need to call a function only once when item.IS_RACING === 1

look

    _.map(recordsets, function(items) {
      return _.map(items, function(item) {
        if (item.IS_RACING === 1) {
          _this.getRacing();
        }
      });
    });

I have that _this.getRacing(); which is being called everytime the conditional is true, but if there is 20 items with IS_RACING === 1, so the function _this.getRacing(); is going to be call 20 times. I need something like, once the app detects when the first IS_RACING === 1 comes up, then fires _this.getRacing(); only once.

Any recommendation ?

Answer:1

As Pointy pointed out (sorry) in the comments, you really don't want to use map() to do this.

Think of the problem in terms of how you would explain it to another developer.

If any of the record sets has an item that is racing, I want to call getRacing().

Now, write code that represents your intent.

var somethingIsRacing = _.some(recordsets, function(items) {
  return _.some(items, function(item) {
    return item.IS_RACING === 1;
  });
});
if(somethingIsRacing) {
  _this.getRacing();
}

This code follows a principle called Command-Query Separation, where you first query to find the information you need using a functional style of programming, then you perform actions that will have side-effects using an imperative programming style.

Answer:2

Situation: I want to get smooth scrolling to anchor links, for every anchor link. Next i want to set an offset for specific anchor links (for example only the links of the navigation, but none of the ...

Situation: I want to get smooth scrolling to anchor links, for every anchor link. Next i want to set an offset for specific anchor links (for example only the links of the navigation, but none of the ...

  1. smooth scrolling anchors
  2. smooth scrolling to page anchors
  3. smooth scrolling navigation with anchors and jquery

Suppose I able to get ($.ajax), process (process_*) and save (store_* =) data A and B independently and already have API: var store_A; function A() { $.ajax({url: "/getA"}).done(function(data) { ...

Suppose I able to get ($.ajax), process (process_*) and save (store_* =) data A and B independently and already have API: var store_A; function A() { $.ajax({url: "/getA"}).done(function(data) { ...

These three functions all work on Chrome, but not IE or Firefox. The first functions adds a cloned row. The second function removes a cloned row. The third function resets the entire field. All ...

These three functions all work on Chrome, but not IE or Firefox. The first functions adds a cloned row. The second function removes a cloned row. The third function resets the entire field. All ...

  1. adding and removing rows and columns in excel

I have been trying to draw a very basic rectangle using THREE.BufferGeometry(). Here is the code - var geometry = new THREE.BufferGeometry(); var material = new THREE.MeshBasicMaterial({color: 'rgb(...

I have been trying to draw a very basic rectangle using THREE.BufferGeometry(). Here is the code - var geometry = new THREE.BufferGeometry(); var material = new THREE.MeshBasicMaterial({color: 'rgb(...