2

So I'm trying to use switch case for this but this doesn't seems to be the way.

I'm trying

switch (obj) {
    case hasPropertyA:
        console.log('hasPropertyA');
        break;
    case hasPropertyB:
        console.log('hasPropertyB');

I was expecting that this does obj.hasPropertyX, and if it receive a true value show the console in any case statement, but not

Anyone have an way to do this? I have many properties do check so I can't just use an if( obj.hasOwnProperty(prop) ) {}, that's why I'm trying switch case statement

4
  • 4
    switch is only for equality tests, not truthy tests. if you have many properties - store them in an array. Commented Nov 12, 2015 at 20:13
  • 1
    Syntactically a switch statement will not save you many lines over an if. Look at it and see if there's a way you can do it with a loop Commented Nov 12, 2015 at 20:14
  • You're thinking of languages with pattern matching, not Javascript. Commented Nov 12, 2015 at 20:24
  • Are all of your objects stored in an array or something like it? Commented Nov 12, 2015 at 21:07

2 Answers 2

13

Well there is a way that you can use switch close to the way you are currently using it. You use a boolean statement as the switch like this:

var car = {
 style: "volvo",
    type: "sport"
}

function checkObjectProperties(obj) {
    switch (true) {
    case (obj.hasOwnProperty("style")):
        console.log("has: property style")
        break;
    case (obj.hasOwnProperty("type")):
        console.log("has property type");
}}

Keep in mind though that the switch-case will break at the first occurance of any true statement. In this case it will break at the first case and it will never enter the second one, even though it also equals as true.

Also always use a default case in the end to catch any objects that wont have any true cases.

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

Comments

3

You're using switch() wrong. You must have a variable or expressions to check as the parameter. An object will not work.

For example, you can do:

var string = "hello";
switch(string) {
    case "green":
        // do something
        break;
    case "hello":
        // do something
        break;

     // and so on...
}

If you had a list of properties, and if you stored them in an array, you can iterate over the array and check if each one is defined:

var properties = ["propertyA", "propertyB", "etc."];
for(var i = 0; i < properties.length; i++) {
    if(obj[properties[i]] != undefined) {
        console.log("Has property: " + properties[i]);
    }
}

You can see a working example on JSFiddle.net.

3 Comments

thanks for your explanation, but I need to check a object that has other properties that I get from a request, than check if this obj has some properties, think this way will not work ;(((
@wtkd yes it does. you iterate over all the strings you were going to put in your hypothetical 'switch' block.
I don't quite understand. Why can't you turn the request into a JS array, and follow this method?

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.