Agnes K. Rivers
Agnes K. Rivers

Reputation: 9

How to check the key in the object is an object

My exercises are use for...in to print out all the keys in the object, print out the keys of the nested objects. But i don't check if the value of nameValue is object

I tried to using for...in but but the result of the value check is undefined

// My Exercises 
var apartment = {
  bedroom: {
    area: 20,
    bed: {
      type: 'twin-bed',
      price: 100
    }
  }
};

// Tried
function checkObj(objs) {
  for (var obj in objs) {
    console.log(obj);
    var check = objs['obj'];
    console.log(check);
  }
}
checkObj(apartment);

Upvotes: 0

Views: 112

Answers (5)

Asaf Aviv
Asaf Aviv

Reputation: 11770

In JavaScript typeof null, typeof [], and typeof {} will return 'object'.

You can check if the constructor.name of the object is equal to 'Object'

function checkObj(objs) {
    for (var obj in objs) {
        var isObj = objs[obj] && objs[obj].constructor.name === 'Object';
        console.log('is Object:', isObj);
    }
}

checkObj(apartment);

As @ZivBen-Or suggested in the comments, you can also check it like this

Object.prototype.toString.call(obj[key]) === "[object Object]"

Upvotes: 1

Hardik Shah
Hardik Shah

Reputation: 4210

Print all keys and values by checking if the dipper level is object or not.

objs[obj] !== null && typeof objs[obj] === 'object' && objs[obj].constructor !== Array

This is the way you can identify if object.

Updated to validate null and array.

// My Exercises 
var apartment = {
    bedroom: {
        area: 20,
        bed: {
        type: 'twin-bed',
        price: 100
        }
    },
    bedroom1: null,
    bedroom2: [],
    bedroom3: {}
};

// Tried
function checkObj(objs) {
    for (var obj in objs) {
     if (objs[obj] !== null && typeof objs[obj] === 'object' && objs[obj].constructor !== Array) {
      checkObj(objs[obj]);
     } else {
      console.log(obj, ':', objs[obj]);
     }
    }
}
checkObj(apartment);

Upvotes: 0

Ziv Ben-Or
Ziv Ben-Or

Reputation: 1194

Here is a recursive function that runs on the object keys and if the value of the proprty is an object it calls the function again. I used Object.prototype.toString.call(obj[key]) === "[object Object]" and not typeof obj[key] === 'object' to check is the property is an object on an object and not and object of Array or a function

var apartment = {
    bedroom: {
        area: 20,
        bed: {
            type: "twin-bed",
            price: 100,
            array: [1,2,3],
            method: function(){}
        }
    }
}

function printObjectKeys(obj) {
    for (var key in obj) {
        console.log(key);
        if (Object.prototype.toString.call(obj[key]) === "[object Object]") {
            printObjectKeys(obj[key]);
        }
    }
}
console.log(printObjectKeys(apartment));

Upvotes: 0

Shubham Dixit
Shubham Dixit

Reputation: 1

In JavaScript, basically everything is an object.Try this method to distinguish the broader Object prototype from an object of key/value pairs ({}).I have given you an example you can try this in your code to see it better.I have appended your code too.

var apartment = {
    bedroom: {
        area: 20,
        bed: {
        type: 'twin-bed',
        price: 100
        }
    }
};

var isPlainObject = function (obj) {
	return Object.prototype.toString.call(obj) === '[object Object]';
};

console.log(isPlainObject(apartment));

console.log(isPlainObject(apartment.bedroom));
console.log(isPlainObject(apartment.bedroom.bed));


// Returns false
console.log(isPlainObject(['wolverine', 'magneto', 'cyclops']))
// returns false
console.log(isPlainObject(null))


// Your code

function checkObj(objs) {
    for (var obj in objs) {
      
        var check = isPlainObject(objs[obj]);
        console.log("your code",check); // this returns true
    }
}
checkObj(apartment);

Upvotes: 0

Kaushik
Kaushik

Reputation: 2090

You can use this function to determine if the value is object or not?

function isObject (item) {
  return (typeof item === "object" && !Array.isArray(item) && item !== null);
}

as typeof [] is also an object

var apartment = {
    bedroom: {
        area: 20,
        bed: {
        type: 'twin-bed',
        price: 100
        },
        test:[1,2,3]
    }
};

function isObject (item) {
  return (typeof item === "object" && !Array.isArray(item) && item !== null);
}

function checkObj(objs) {
    for (var obj in objs) {
        console.log(obj);
        var check = isObject(objs[obj]);
        console.log(check);
    }
}
checkObj(apartment);

Reference

Upvotes: 0

Related Questions