0

I want to look through an object and assign each of it's existent properties to a variable.

There are 4 possible properties. Some of the objects have all 4. Some might only have two.

How can I check if a particular property exists? Is there an equivalent of indexOf() for arrays but for objects instead?

gloopit
  • 437
  • 2
  • 9
  • 2
    It is really tough to imagine your code from just plain text. Care to reveal the relevant source code here? This is so that we can better help you. – Samuel Toh Aug 18 '16 at 01:44
  • 3
    Have you tried `hasOwnProperty` or converting the object to array with `Object.keys`? – litel Aug 18 '16 at 01:45
  • if the properties can't be false (eg they are all sub-objects), then duck typing is readable and fast : `if(obj.key1); alert(obj.key1.name);` – dandavis Aug 18 '16 at 01:54
  • `false` or [_falsey_](http://stackoverflow.com/a/19839953/1848578), including `0` and other common values – qxz Aug 18 '16 at 02:02
  • @qxz: yeah, that's what i meant, good call – dandavis Aug 18 '16 at 02:06

4 Answers4

3

Use the in keyword:

"key" in object

which returns true or false, depending if the object, or anything in its prototype chain, has that property.

You can also use object.hasOwnProperty("key"), which will only be true if the object has key as a property of itself, not its prototype. Example:

var object = {};
"toString" in object; // true
object.hasOwnProperty("toString"); // false

Note (as per @dandavis's comment) that if object has a custom property called hasOwnProperty, this gets thwarted; to work around this, use hasOwnProperty.call(object, "key"). Example:

var a = {hasOwnProperty: Boolean};
a.hasOwnProperty('name'); // true
hasOwnProperty.call(a, 'name'); // false
qxz
  • 3,814
  • 1
  • 14
  • 29
1

If you are only interested in properties set directly on the object (not accessible via the prototype chain) then hasOwnProperty will provide a boolean value, true, if an object has the specified property.

For example: testObject.hasOwnProperty('propertyToCheckFor') would return true if testObject.propertyToCheckFor exists, otherwise it would be false.

See the following code for a more expanded example:

var obj1 = {
  a: 1
};
var obj2 = {
  a: 1,
  b: 2
};
var obj3 = {
  b: 2,
  c: 3
};
var obj4 = {
  a: 1,
  b: 2,
  c: 3
};


// For dispaly purposes
document.write('<pre>' + JSON.stringify({
  obj1: {
    hasA: obj1.hasOwnProperty('a'),
    hasB: obj1.hasOwnProperty('b'),
    hasC: obj1.hasOwnProperty('c')
  },
  obj2: {
    hasA: obj2.hasOwnProperty('a'),
    hasB: obj2.hasOwnProperty('b'),
    hasC: obj2.hasOwnProperty('c')
  },
  obj3: {
    hasA: obj3.hasOwnProperty('a'),
    hasB: obj3.hasOwnProperty('b'),
    hasC: obj3.hasOwnProperty('c')
  },
  obj4: {
    hasA: obj4.hasOwnProperty('a'),
    hasB: obj4.hasOwnProperty('b'),
    hasC: obj4.hasOwnProperty('c')
  }
}, null, 2) + '</pre>');
Jason Cust
  • 10,743
  • 2
  • 33
  • 45
  • `{hasOwnProperty: Boolean}` ? – dandavis Aug 18 '16 at 01:57
  • @dandavis What? If someone does that then they deserve the 7th ring of hell they find themselves in. – Jason Cust Aug 18 '16 at 01:59
  • 1
    i thought so too, until i found it in the wild on an excel spreadsheet about real estate that was converted to JSON. it wasn't a function, so it just threw, but it made me super anal about checking object membership... – dandavis Aug 18 '16 at 02:02
0

var obj = {
  foo: 1,
  bar: 2,
  baz: 3
}

Object.keys(obj).forEach(function(key) {
  window[key] = obj[key]
})

console.log(foo, bar, baz)

Or in ES2015

const obj = {
  foo: 1,
  bar: 2,
  baz: 3
}

function assignPrivateVars() {
  let {foo, bar, baz} = obj;
  console.log(foo, bar, baz);
}

assignPrivateVars();
cstuncsik
  • 2,698
  • 2
  • 16
  • 20
0

You can use destructuring assignment. If value is not defined, variable will be set to undefined. You can also check if variable is defined after destructuring then delete variable by reference.

var data = {a:1, b:2, c:3};

var {a, b, c, d} = data; // `d`: `undefined`
guest271314
  • 1
  • 15
  • 104
  • 177
  • Note that this an an ES6 feature, and doesn't have wide support: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Browser_compatibility – csander Aug 18 '16 at 01:59
  • that's really neat and clean, but i don't think it's exactly what OP needs because it's just as complicated (if not more so) to check for a lexical as a member. – dandavis Aug 18 '16 at 02:00