1

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);
Shubham Dixit
  • 9,242
  • 4
  • 27
  • 46

5 Answers5

1

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]"
Asaf Aviv
  • 11,279
  • 1
  • 28
  • 45
0

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

Kaushik
  • 2,072
  • 1
  • 23
  • 31
0

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);
Hardik Shah
  • 4,042
  • 2
  • 20
  • 41
  • it wouldn't work correct if the value of the property is an `Array` or `function` `Object` – Ziv Ben-Or Jun 20 '19 at 06:43
  • @ZivBen-Or, OP has already shared sample JSON and OP already said that him exercise is completed. According to OP sample JSON, this is the enough and minimal condition to identify an object. – Hardik Shah Jun 20 '19 at 06:48
  • How to check value is array or object or fucntion object and null – Agnes K. Rivers Jun 20 '19 at 06:49
  • @AgnesK.Rivers, then you should include your requirement in question. Please update the question. I will try to update the answer accordingly. – Hardik Shah Jun 20 '19 at 06:50
0

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);
Shubham Dixit
  • 9,242
  • 4
  • 27
  • 46
0

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));
Ziv Ben-Or
  • 1,149
  • 6
  • 15