I'm looking to create a function that can determine whether an array contains only objects. Here's what I came up with:
function checkArrayForObjects(arr){
return arr.join("").replace(/\[object Object\]/g, "") === "";
}
Here's an example of how you would use this function:
// true
checkArrayForObjects([
{"name" : "Alice", "age" : 30},
{"name" : "Bob", "age" : 25}
]);
// false
checkArrayForObjects([
{"name" : "Alice", "age" : 30},
"name=Bob;age=25"
]);
Is there a more efficient way to achieve this? Is relying on the literal "[object Object]"
secure for checking purposes? I'd also prefer a solution without jQuery.