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.

6
  • 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). Commented Jul 14, 2015 at 3:57
  • Yes, Array and Function are also excluded. Array must contains literal objects only. Commented Jul 14, 2015 at 4: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. Commented Jul 14, 2015 at 4:10
  • Ah, you're right!it didn't cross my mind. I might consider your comment. Commented Jul 14, 2015 at 4: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? Commented Jul 14, 2015 at 4:27

2 Answers 2

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]';
  });
}
Sign up to request clarification or add additional context in comments.

6 Comments

Please note that for null, undefined elements, this also returns true.
In what environment? Not by spec. See MDN for more info.
Object.prototype.toString.call(null) returns [Object Null] and Object.prototype.toString.call(undefined) returns [Object Undefined].
Close, [object Null]. But that's the point. Why do you say this returns true?
Oh are you saying that the original implementation returned true for those?
|
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.