Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if object has a set of properties in javascript

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

like image 295
Ben Avatar asked Dec 14 '13 01:12

Ben


People also ask

How do you check if an object has a property in TypeScript?

To check if a property exists in an object in TypeScript: Mark the specific property as optional in the object's type. Use a type guard to check if the property exists in the object. If accessing the property in the object does not return a value of undefined , it exists in the object.

Is property in object JavaScript?

JavaScript is designed on a simple object-based paradigm. An object is a collection of properties, and a property is an association between a name (or key) and a value. A property's value can be a function, in which case the property is known as a method.

How do you check if an object has a method in JavaScript?

Use the typeof operator to check if an object contains a function, e.g. typeof obj. sum === 'function' . The typeof operator returns a string that indicates the type of the value.


2 Answers

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;
  };
}
like image 93
p.s.w.g Avatar answered Sep 21 '22 18:09

p.s.w.g


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
}
like image 34
Dexygen Avatar answered Sep 22 '22 18:09

Dexygen