0

I have an object with following markup :

Object {workout: Object}
   workout: Object
   1/12/2015: Array[3]
       0: "workoutTitle0"
       1: "workoutTitle1"
       2: "workoutTitle2"
    2/12/2015: Array[3]
    3/12/2015: Array[3]
    4/12/2015: Array[3]
    5/12/2015: Array[3]
    6/12/2015: Array[3]
    7/12/2015: Array[3]

I want to check if for example the field 10/12/2015 exists - if exists I want to load array that this field is holding, if not create new empty array for that field.

I tried this code but I get false everytime, even when field do not exist:

function isEmpty(obj) {
    var prop = '10/12/2015'
    for( prop in obj) {
        if(obj.hasOwnProperty(prop))
            return false;
    }
    return true;
}

Here if JSFiddle :

https://jsfiddle.net/x9dnwgwc/3/

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
marcinf2
  • 83
  • 11
  • 1
    If you're interacting with the object in memory, it's not JSON, it's just an object. JSON is a *textual* notation for data exchange. – T.J. Crowder Mar 19 '15 at 17:14
  • Your browser has a fully-featured debugger built into it: Use the menus to open the debugger, or on most browsers it's F12 and/or Ctrl+Shift+I (Cmd+Shift+I on Macs). The debugger will let you execute your code a line at a time ("single-step" through your code) and let you see what's in your variables as you step through. Very useful for understanding what's going on. – T.J. Crowder Mar 19 '15 at 17:20

1 Answers1

2

I want to check if for example the field 10/12/2015 exists

You're working too hard, it's just:

if (obj.hasOwnProperty('10/12/2015')) {
    // It exists in the object itself
}

...for an "own" property, or

if ('10/12/2015' in obj) {
    // It exists in the object or one of its prototypes
}

...for a property that's it's own or it inherits from a prototype.

I tried this code but I get false everytime, even when field do not exist:

function isEmpty(obj) {
    var prop = '10/12/2015'
    for( prop in obj) {
        if(obj.hasOwnProperty(prop))
            return false;
    }
    return true;
}

Your

var prop = '10/12/2015';

line declares the prop variable, and assigns it the value '10/12/2015'; but then you immediately overwrite that value with:

for( prop in obj) {

The first thing the for-in loop will do is overwrite the value of prop.

What your loop was really doing was checking to see if the object had any "own" properties (if so, it returned false) or only properties it inherited from its prototype(s) (if so, it returned 'true').

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Checked again , and still something is messed up https://jsfiddle.net/x9dnwgwc/4/ It returns true , even if the prop exists in object ... – marcinf2 Mar 19 '15 at 17:43
  • @marcinf2: You're looking on `jsonObj`. It's not on `jsonObj`, it's on `jsonObj.workout`: https://jsfiddle.net/x9dnwgwc/6/ (And again, just as a side note, JSON isn't involved there. It's just an object.) – T.J. Crowder Mar 19 '15 at 18:00
  • @marcinf2: That `if(obj.hasOwnProperty(prop)) { return false; } else { return true; }` can, of course, be written `return !obj.hasOwnProperty(prop);` – T.J. Crowder Mar 19 '15 at 18:02