0

I want to make sure that two properties (referencing objects) connected and connections both contain at least one property, like this:

if ((Object.keys(layer[i1].connected).length > 0 
&& Object.keys(layer[i1].connections).length > 0) || /* other logic omitted */) {
    //do stuff
}

That works so far. But I realized I need to make sure not only that properties exist within connected and connections but also that those contents contain the property active which is equal to true.

So the contents need to be:

connections : { //at least one is true
    34 : {
        active : true 
    },
    37 : {
        active : false
    },
    38 : {
        active : false
    }
}

and (&&)

connected : { //at least one is true
    12 : {
        active : true
    },
    22 : {
        active : true
    }
}

not

connections : {
    34 : {
        active : false
    },
}

or

connected : {}

I'm just wondering if there's a simple, elegant way to perform this kind of check rather than the messy options I'm thinking of. Is there an elegant way to do this? Obviously there are many ways to do it, but I'm really interested in a one-line approach, similar to how Object.keys very neatly exposes the number of properties.

0

3 Answers 3

1

What you want is pretty specific, so why not just make your own helper function:

const hasActiveProp = obj => Object.keys(obj).some(k => obj[k].active)

Then you can just do:

if(hasActiveProp(layer[i1].connected) && hasActiveProp(layer[i1].connections)){
  // do something here
}

Just to note though that in order to make your code elegant, I wouldn't focus on your specific problem, but rather why you need to perform this check. You should figure out if there is any way you can get the data in a different format so that the check becomes more intuitive.

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

2 Comments

That actually is a much more elegant check than what I had in mind. Thanks. Edit: have to award the answer to nnnnn though since his is essentially the same and was submitted first!
@Viziionary - It wouldn't bother me if you accepted this answer. It's a bit more complete in that I only suggested encapsulating the test in a function, whereas this answer actually showed the function...
0

The shortest way I can think to apply your test condition is like this:

Object.keys(layer[i1].connections).some(p=>layer[i1].connections[p].active)

I don't consider that particularly elegant, but at least it is still a single expression.

I'd encapsulate that in a function so that you can just call the function twice from within your if condition.

2 Comments

By the way I couldnt think of the proper way to title what my question really is, if you'd like to edit the title to something less vague Ill happily accept the edit.
OK, well I edited the title to make it much more specific. Not sure if the new title is elegant, though.
0

Here is a fun way to do it in 1 line:) If you are good with regex, you can handle all the cases with 1 test, and you don't need check obj availability.

var obj = {
  connections: { //at least one is true
    34: {
      active: true
    },
    37: {
      active: false
    },
    38: {
      active: false
    }
  },
  connected: { //at least one is true
    12: {
      active: true
    },
    22: {
      active: false
    }
  }
}
console.log(JSON.stringify(obj))

console.log(/{"active":true}/.test(JSON.stringify(obj.connections)) && /{"active":true}/.test(JSON.stringify(obj.connected)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.