0

So I'm trying to figure out how to execute a function if all of the if/else statement are false. Below is a snippet of code that I'm currently working that checks the value within the object using a for loop to iterate each object then checks the key/pair value technology.

If all of the statement are false it should return or execute the function noMatch The current snippet executes the function noMatch for every else statement. Is there a way to just execute it once if none of the values matched?

$(document).ready(function() {
    let object = [
        {
            "planId": "1",
            "technology": "LTE@Home"
        },
        {
            "planId": "2",
            "technology": "LTE@Home"
        },
        {
            "planId": "54",
            "technology": "home-prepaid-wifi"
        },
        {
            "planId": "0",
            "technology": "XTreme Prepaid",
        }
    ];


    let technology = "lte-technology";
    let dataObject = object;

    function match() {
        console.log("match");
    }

    function noMatch() {
        console.log("no match");
    }

    for (let key in dataObject) {
        let obj = dataObject[key];


        if(technology == obj.technology) {
            return match()

        } else {
            return noMatch()
        }
    }    

});
random_user_name
  • 25,694
  • 7
  • 76
  • 115
clestcruz
  • 1,081
  • 3
  • 31
  • 75
  • 4
    You'd typically use a flag initialised to false that's set to true if any condition is true. Then at the end of processing, if it's still false, then all tests were false and you can do whatever with that information. What have you tried? – RobG Oct 10 '19 at 01:54
  • @RobG I've been trying to figure how to do that. Was thinking of doing another `if/else` statement that checks the loop count and once it reaches the last count it will return or execute a function – clestcruz Oct 10 '19 at 02:00
  • 1
    the answers that use `array.some` are the best. the array prototype functions both express intent and make code more concise. – Chris Rollins Oct 10 '19 at 02:31

5 Answers5

3

I think the Array prototype method some works well in this instance:

const technology = "lte-technology";
const hasMatch = dataObj.some(obj => obj.technology === technology);
Greg Brodzik
  • 1,765
  • 7
  • 9
2

Most concise way is probably Array.some (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some)

let hasMatch = dataObject.some(x => x.technology === technology);
tymeJV
  • 103,943
  • 14
  • 161
  • 157
2

Hi you could do something like this, using Array.some:

(() => {
    const items = [
        {
            "planId": "1",
            "technology": "LTE@Home"
        },
        {
            "planId": "2",
            "technology": "LTE@Home"
        },
        {
            "planId": "54",
            "technology": "home-prepaid-wifi"
        },
        {
            "planId": "0",
            "technology": "XTreme Prepaid",
        }
    ];
   
    const match = () => console.log("match");
    const noMatch = () => console.log("no match");
    const technology = "LTE@Home";    
    const hasMatch = items.some(x => x.technology === technology);
      
    return hasMatch ? match() : noMatch();     
})();
rcoro
  • 326
  • 6
  • 12
1

I was unable to find a duplicate, although I'm certain one exists.

Per the comment by RobG, you set a flag, and check for the flag once your loop is done.

Note that there's probably a smoother way using on of javascript's many array and object methods, but this is your code modified to do what you want:

let dataObject = [{
    "planId": "1",
    "technology": "LTE@Home"
  },
  {
    "planId": "2",
    "technology": "LTE@Home"
  },
  {
    "planId": "54",
    "technology": "home-prepaid-wifi"
  },
  {
    "planId": "0",
    "technology": "XTreme Prepaid",
  }
];

let technology = "lte-technology";

// set flag to false
let hasMatch = false;
for (let key in dataObject) {
  let obj = dataObject[key];

  // if the property matches, set flag to true
  // (never use ==  always use ===)
  if (technology === obj.technology) {
    hasMatch = true;
    // for performance, may as well stop the loop...
    break;
  }
}

if (hasMatch) {
  console.log('match');
} else {
  console.log('nomatch');
}

Note my comment about using === instead of == - for more info, see this Q&A

random_user_name
  • 25,694
  • 7
  • 76
  • 115
0

This should help.

    let object = [
        {
            "planId": "1",
            "technology": "LTE@Home"
        },
        {
            "planId": "2",
            "technology": "LTE@Home"
        },
        {
            "planId": "54",
            "technology": "home-prepaid-wifi"
        },
        {
            "planId": "0",
            "technology": "XTreme Prepaid",
        }
    ];


    let technology = "lte-technology";
    let dataObject = object;

    function match() {
        console.log("match");
    }

    function noMatch() {
        console.log("no match");
    }
    let ok = false; 
    for (let key in dataObject) {
        let obj = dataObject[key];


        if(technology == obj.technology) {
            ok= true; 
             break;
        } 

    }
             if(ok)
           return match();
         return noMatch();     

});

AllMightyGoat
  • 49
  • 1
  • 6