3

I want to check if an array contains only objects. So I created this function:

function arrayContainsObjOnly(arr){
  return arr.join("").replace(/\[object Object\]/g, "") === "";
}

Here is how you would use it:

// return true
arrayContainsObjOnly([
  {"name" : "juan", "age" : 28},
  {"name" : "pedro", "age" : 25}
]);

// return false
arrayContainsObjOnly([
  {"name" : "juan", "age" : 28},
  "name=pedro;age=25"
]);

Is there any cleaner way to do this? Is using the literal "[object Object]" for checking is safe? I also prefer a non-jQuery solution.

nameless
  • 88
  • 4
  • If any object in the array overrides `.toString()` to provide a custom implementation, then this won't work. Also, this would not include Array and Function which are each a type of object (not sure if you want to include them or not). – jfriend00 Jul 14 '15 at 03:57
  • Yes, Array and Function are also excluded. Array must contains literal objects only. – nameless Jul 14 '15 at 04:05
  • 1
    Here's an actual glitch. If one of the array elements is the literal string `"[object Object]"`, you will get a false positive. So, your method is NOT entirely safe. You will probably have to check that both `typeof elem === "object"` and that `elem.toString() === "[object Object]"` for each item to be entirely safe. – jfriend00 Jul 14 '15 at 04:10
  • Ah, you're right!it didn't cross my mind. I might consider your comment. – nameless Jul 14 '15 at 04:23
  • 1
    Do you intend this to return `true` if some of the elements are `null` or `undefined` or is that an accident of your implementation? – Scott Sauyet Jul 14 '15 at 04:27
  • @scott - null or undefined are also not accepted, i never thought that those are considered as object. Thanks for pointing out. :) – nameless Jul 14 '15 at 05:35

2 Answers2

1

Conceptually simpler and cleaner, but no less code:

function arrContainsObjOnly(arr) {
  return arr.every(function(el) {
    return Object.prototype.toString.call(el) === '[object Object]';
  });
}

Update

On second thought, this variant would be better, as it would return false on encountering the first non-object:

function arrContainsObjOnly(arr) {
  return !arr.some(function(el) {
    return Object.prototype.toString.call(el) !== '[object Object]';
  });
}
Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
  • Please note that for **null**, **undefined** elements, this also returns true. – TaoPR Jul 14 '15 at 04:15
  • In what environment? Not [by spec](http://www.ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring). See [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString#Using_toString%28%29_to_detect_object_class) for more info. – Scott Sauyet Jul 14 '15 at 04:20
  • `Object.prototype.toString.call(null)` returns `[Object Null]` and `Object.prototype.toString.call(undefined)` returns `[Object Undefined]`. – TaoPR Jul 14 '15 at 04:22
  • 1
    Close, `[object Null]`. But that's the point. Why do you say this returns true? – Scott Sauyet Jul 14 '15 at 04:24
  • Oh are you saying that the original implementation returned true for those? – Scott Sauyet Jul 14 '15 at 04:25
  • Sorry, @Scott, I missed the point. Carefully considered you answer and yep, that works pretty well. This is much simplified than I thought. it deserves to be a final answer. – TaoPR Jul 14 '15 at 04:28
0

you can use typeof operator

try this one

// CASE 1  :

var arrItem = [
    {"name" : "juan", "age" : 28},
    {"name" : "pedro", "age" : 25},
    "name=pedro,age=25"
]

var boolIsAllContainObject = arrItem.every(function(oneItem,key){ 

    if ((typeof oneItem) === "object") { 
        return true;
    } else {
        return false;
    }
});

console.log(boolIsAllContainObject)   //return false as one element is not object


// CASE 2:

var arrItem = [
    {"name" : "juan", "age" : 28},
    {"name" : "pedro", "age" : 25},
    {"name" : "name=pedro,age=25"}
]

var boolIsAllContainObject = arrItem.every(function(oneItem,key){ 

    if ((typeof oneItem) === "object") { 
        return true;
    } else {
        return false;
    }
});

console.log(boolIsAllContainObject)   //return true allelement are object
Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
RIYAJ KHAN
  • 15,032
  • 5
  • 31
  • 53