node.js - How to check value existence as hasOwnProperty JSON NodeJs -
i've found how check key in json object :
var myjson = {'key':'value', 'key2':'value2'}; if(myjson.hasownproperty('key2')){ //do if key exist }
now, how can check value2 exist ? hasownvalue exists ?
as molda suggests, there no way find out whether object contains value, other looping on fields.
pure js
var myobject = {"a": 1, "b": 2}; var valueimlookingfor = 2; (var key in myobject) { if (myobject[key] === valueimlookingfor) { console.log('yay, myobject contains', valueimlookingfor); } }
there libraries kind of stuff though. using lodash' includes() becomes easy:
_.includes(myobject, valueimlookingfor); // true
Comments
Post a Comment