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 provided callback function once each element present in array until finds 1 callback returns falsy value (a value becomes false when converted boolean). if such element found, every method returns false. otherwise, if callback returned true value elements, every return true. 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

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -