finding undefined or null value from object inside object

javascript object property exists but is undefined
javascript check null or undefined
javascript undefined error
how to handle undefined in javascript
typeof javascript
javascript check if null or empty
javascript check if undefined
javascript check for null or empty string

i would like to push keys inside array if found undefined or null

const obj = {
  name:'ab',
  edu:'av',
  degres:{
    a1:'',
    b1:'1'
  },
  platform:undefined
 }

i want an output like

  `['a1','platform']`

as the value for a1 and platform were null and undefined

i have treid this solution but it doesnt work

 function iterater(obj){
  let blankValues = [];
  Object.keys(obj).map((key) => {
      if (obj.hasOwnProperty(key) && (typeof obj[key] === "object")) {
        iterater(obj[key])
      } else {
          if (typeof obj[key] === "undefined" || obj[key] === ''){
            blankValues.push(key);
         }
      }
    })
  return blankValues;

}

but this somehow only return ['platform'] only,but the expected output should be ['platform','a1'],i think when running iterater(obj[key]),the value of array (blankValues) gets blank as it doesnt perserve it,but please help me with appropriate logic and structure

The issue is because you're re-defining blankValues as an empty array in every iteration of the recursive loop. To fix this you could accept the array as an optional argument of the function so that values get pushed to it on each iteration.

Also note that, as @ziggy wiggy pointed out in the comments, your logic will fail when a null value is encountered as typeof obj[key] === "object" would be true. You need a specific null check too.

const obj = {
  name: 'ab',
  edu: 'av',
  degres: {
    a1: '',
    b1: '1'
  },
  platform: undefined,
  foo: null
}

function iterater(obj, arr) {
  arr = arr || [];
  Object.keys(obj).map((key) => {
    if (obj.hasOwnProperty(key) && (typeof obj[key] === "object") && obj[key] !== null) {
      iterater(obj[key], arr)
    } else {
      if (typeof obj[key] === "undefined" || obj[key] === null || obj[key].trim() === '') {
        arr.push(key);
      }
    }
  })
  return arr;
}

console.log(iterater(obj));

7 Tips to Handle undefined in JavaScript, And set a default value (an empty object { } ) for the config parameter, to skip the second argument� isNaN ( 1 + null ) //false. isNaN ( 1 + undefined ) //true. So, when it comes to the difference in type, “null” is an object with a valid value having no properties, is non-mutable and a single instance exists in the system all the time. One can verify the nature of “null”, by using “type of” operator.

As you said yourself, when you call iterater(obj[key]) it sets a new local blankValues and puts values in it. So i think you should put blankValues outside the function. And then you don't have to return it (or you can if you want it as a return value).

Or you can pass blankValues as a parameter of iterater in both the main call and the "inside" call

null, The value null represents the intentional absence of any object value. JavaScript Demo: Standard built-in objects - Null a literal: null . null is not an identifier for a property of the global object, like undefined can be. TypeError: cannot use 'in' operator to search for 'x' in 'y' � TypeError: cyclic object value� var test = undefined; console.log(test); // undefined. Looking up non-existent properties in an object: var test = {}; console.log(test.prop); // undefined Summarize it for me, Brandon. Here are the quick facts: null is an assigned value. It means nothing. undefined means a variable has been declared but not defined yet. null is an object.

You need to consume the result of recursive call. For example add it back to blankValues like this blankValues.push(...iterater(obj[key]))

const obj = {
  name:'ab',
  edu:'av',
  degres:{
    a1:'',
    b1:'1'
  },
  platform:undefined
 }

 function iterater(obj){
  let blankValues = [];
  Object.keys(obj).map((key) => {
      if (obj.hasOwnProperty(key) && (typeof obj[key] === "object")) {
        blankValues.push(...iterater(obj[key]))
      } else {
          if (typeof obj[key] === "undefined" || obj[key] === ''){
            blankValues.push(key);
         }
      }
    })
  return blankValues;

}

console.log(iterater(obj))

How to Determine If Variable is Undefined or NULL in JavaScript, In simple words you can say a null value means no value or absence of a value as expected, because JavaScript return "object" for typeof null instead of "null". The pairs () function turns the input object into an array of key/value arrays. You do this so that it’s easier to use reject () to eliminate undefined and null values. After, you’re left with pairs that weren’t rejected, and these are input for zipObject (), which reconstructs your object for you.

You must push the result returned by the recursive call to your array. Change:

iterater(obj[key])

for:

blankValues.push(...iterater(obj[key]))

const obj = {
  name: 'ab',
  edu: 'av',
  degres: {
    a1: '',
    b1: '1'
  },
  platform: undefined
}

 function iterater(obj){
  let blankValues = [];
  Object.keys(obj).map((key) => {
      if (obj.hasOwnProperty(key) && (typeof obj[key] === "object")) {
        blankValues.push(...iterater(obj[key]))
      } else {
          if (typeof obj[key] === "undefined" || obj[key] === ''){
            blankValues.push(key);
         }
      }
    })
  return blankValues;

}

console.log(iterater(obj));

null and undefined in JavaScript, In the above example, null is assigned to a variable myVar. It means we have " object". If you try to find DOM element using document. A variable or an object has an undefined value when no value is assigned before using it. So you can� In almost all other languages, accessing a variable that does not exist it will crash the program. In JavaScript the program is allowed to continue in the hope that

You could take a check for falsy keys and return the key, if the property is an object, the check the object.

const
    getFalsy = o => Object.keys(o).reduce((r, k) => {
        if (!o[k]) return [...r, k];
        if (typeof o[k] === 'object') return [...r, ...getFalsy(o[k])];
        return r;
    }, []),
    object = { name: 'ab', edu: 'av', degres: { a1: '', b1: '1' }, platform: undefined };

console.log(getFalsy(object));

Null vs. Undefined - TypeScript Deep Dive, Interestingly in JavaScript with == , null and undefined are only equal to each its value to null before encoding and transmitting the object to a remote store. Plain objects also support similar methods, but the syntax is a bit different. Object.keys, values, entries. For plain objects, the following methods are available: Object.keys(obj) – returns an array of keys. Object.values(obj) – returns an array of values. Object.entries(obj) – returns an array of [key, value] pairs.

The Null Variable, an undefined variable; a null pointer; a null object For example, suppose you wanted to find the subscripts of both the minimum and maximum values in an� null is an object, empty is a string, undefined is undefined. It is true that null==undefined, but false when it comes to null===undefined. We can safely use xxx==null to check both null and undefined. Interestingly, an empty string is totally not equals to null and undefined.

JavaScript Data Types, JavaScript variables can hold many data types: numbers, strings, objects and more: var length You can use the JavaScript typeof operator to find the type of a JavaScript variable. undefined and null are equal in value but different in type:. I have an aspx page where I am using jquery to open a modal popup with aspxGridview to show some data. It works fine on chrome, firefox and other but crashes on IE 7.

How to Check for null in JavaScript, This contrasts null from the similar primitive value undefined , which is an unintentional absence of any object value. That is because a variable�

Comments
  • You're using .map() as though it was .forEach(), so it's creating a new array, filling it with undefined and then throwing it away. Also, you don't need the .hasOwnProperty test, since Object.keys() will only give you "own" properties.
  • ...and an empty string is distinct from null in JS.
  • oh yes true got you,Thanks,and there any easy or short cut solution with power of es6
  • No problem, glad to help. You could potentially shorten the code by recursively using reduce() on the keys of each object (I've not tested it), but that code would be very difficult to follow. In terms of readability/shortness this is ideal, IMO.
  • Recursively using reduce() or even map() or forEach() would do the job yes, but as Rory greatly said, this solution is the best in term of readability.
  • Keep in mind that this will fail if there's ever a null value, since typeof obj[key] === "object" will be true, so Object.keys(null) will error.
  • @ziggywiggy thank you. I updated the answer with regards to that