Underscore: Validate error, array is _.isObject() true?

Created on 25 Nov 2013  ·  8Comments  ·  Source: jashkenas/underscore

var object = {a: 1, b: 2};
console.log('object: has object', _.isObject(object)); //true
console.log('object: has array', _.isArray(object)); //false
console.log('object: has', toString.call(object)); //[object Object]

var object2 = {t: [{a: 1, b: 2}, {c: 3, d: 4}]};
console.log('object2: has object', _.isObject(object)); //true
console.log('object2: has array', _.isArray(object)); //false
console.log('object2: has', toString.call(object2)); //[object Object]

var array = [1, 2];
console.log('array: has object', _.isObject(array)); //true
console.log('array: has array', _.isArray(array)); //true
console.log('array: has', toString.call(array)); //[object Array]

var array2 = [{a: 1, b: 2}, {c: 3, d: 4}];
console.log('array2: has object', _.isObject(array2)); //true
console.log('array2: has array', _.isArray(array2)); //true
console.log('array2: has', toString.call(array2)); //[object Array]

Most helpful comment

I think he was looking for

_.isPlainObject([1,2]) // false

All 8 comments

Hi @afranioce! That looks about right to me. Which part would you like to contest?

The type should not be false?

//using jQuery
jQuery.type ([1, 2]) //array

I'm not sure what you mean, but I think you're implying that _.isObject([]) should return false. This is not the case as arrays are most certainly objects.

I think he was looking for

_.isPlainObject([1,2]) // false

@dberringer That's a lodash or jquery method that doesn't exist in Underscore at the moment.

Yes. I should have been more specific. He was looking for a method like isPlainObject. Thanks.

_.isPlainObject +1 for sure. why not? its so useful!!

+1 why not? is there another way/method in underscore for that?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

serut picture serut  ·  14Comments

mjeanroy picture mjeanroy  ·  15Comments

jashkenas picture jashkenas  ·  14Comments

siyegen picture siyegen  ·  13Comments

sky0014 picture sky0014  ·  8Comments