Reputation: 4297
Let's say I have an object named a
, how could I check that a
has a specific list of multiple properties in shorthand, I think it can be done using in logical operator,
Something like this:
var a = {prop1:{},prop2:{},prop3:{}};
if ({1:"prop1",2:"prop2",3:"prop3"} in a)
console.log("a has these properties:'prop1, prop2 and prop3'");
EDIT
If plain javascript can't help, jQuery will do, but i prefer javascript
EDIT2
Portability is the privilege
Upvotes: 18
Views: 26917
Reputation: 3269
Slightly more elegant use of the Object.every()
prototype function to include try-catch
:
try {
const required = ['prop1', 'prop2', 'prop3']
const data = {prop1: 'hello', prop2: 'world', prop3: 'bad'}
if (!required.every( x => x in data )) throw new Error('missing property')
console.log('all properties found')
} catch(err) {
console.log(err.message)
}
``
Upvotes: 1
Reputation: 149078
The simplest away is to use a conventional &&
:
if ("prop1" in a && "prop2" in a && "prop3" in a)
console.log("a has these properties:'prop1, prop2 and prop3'");
This isn't a 'shorthand', but it's not that much longer than what you've proposed.
You can also place the property names you want to test in an array and use the every
method:
var propertiesToTest = ["prop1", "prop2", "prop3"];
if (propertiesToTest.every(function(x) { return x in a; }))
console.log("a has these properties:'prop1, prop2 and prop3'");
Note however, that this was introduced in ECMAScript 5, so it is not available on some older browsers. If this is a concern, you can provide your own version of it. Here's the implementation from MDN:
if (!Array.prototype.every) {
Array.prototype.every = function(fun /*, thisp */) {
'use strict';
var t, len, i, thisp;
if (this == null) {
throw new TypeError();
}
t = Object(this);
len = t.length >>> 0;
if (typeof fun !== 'function') {
throw new TypeError();
}
thisp = arguments[1];
for (i = 0; i < len; i++) {
if (i in t && !fun.call(thisp, t[i], i, t)) {
return false;
}
}
return true;
};
}
Upvotes: 37
Reputation: 3967
I think it's good to try it:
/* Create an object class */
var obj = function(){ this.attributes = new Array(); }
obj.prototype.addProp = function(value){ this.attributes.push(new attribute(value)); }
obj.prototype.hasProp = function(value){
for(var i = 0; i < this.attributes.length; i++){
if(value == this.attributes[i].value) return true; } return false; }
function attribute(value){
this.value = value;
}
/* Testing object has some value*/
var ob = new obj();
ob.addProp('1');
ob.addProp('2');
ob.addProp('3');
//* check value index
//alert(ob.attributes[0].value);
//* check if ob has prop
alert(ob.hasProp('1'));
Here is DEMO
Upvotes: 0
Reputation: 12561
This is where the underscore.js library really shines. For instance it provides an already poly-filled every()
method as suggested in a comment to p.s.w.g.'s answer: http://underscorejs.org/#every
But there's more than one way to do it; the following, while more verbose, may also suit your needs, and exposes you to more of what underscore can do (e.g. _.keys and _.intersection)
var a = {prop1:{},prop2:{},prop3:{}};
var requiredProps = ['prop1', 'prop2', 'prop3'];
var inBoth = _.intersection(_.keys(a), requiredProps);
if (inBoth.length === requiredProps.length) {
//code
}
Upvotes: 7
Reputation: 21842
Use Array.reduce like this:
var testProps = ['prop1', 'prop2', 'prop3'];
var a = {prop1:{},prop2:{},prop3:{}};
var result = testProps.reduce(function(i,j) { return i && j in a }, true);
console.log(result);
Upvotes: 6
Reputation: 10617
Like this:
var testProps = ['prop1', 'prop2', 'prop3', 'prop4'];
var num = -1, outA;
for(var i in a){
if(i === testProps[++num])outA[num] = i;
}
console.log('properties in a: '+outA.join(', '));
Upvotes: 1