How to determine equality of values of JavaScript object? -
in given object values of properties same.
var obj = { property1: 'some value', property2: 'some value', property3: 'some value', property4: 'some value' }
the function checkequality should return true if values same , false otherwise.
i can following achieve it:
function checkequality () { var compare = obj.propery1; (var key in obj) { if (obj[key] !== compare) { return false; } } return true; }
but solution far not best.
you use array#every
it.
the
every
method executes providedcallback
function once each element present in array until finds 1callback
returns falsy value (a value becomesfalse
when converted boolean). if such element found,every
method returnsfalse
. otherwise, ifcallback
returnedtrue
value elements,every
returntrue
.callback
invoked indexes of array have assigned values; not invoked indexes have been deleted or have never been assigned values.
var obj = { propery1: 'some value', propery2: 'some value', propery3: 'some value', propery4: 'some value' }, equal = object.keys(obj).every(function (k, i, kk) { return !i || obj[kk[0]] === obj[k]; }); console.log(equal);
Comments
Post a Comment