How to check if a value exists in an object using JavaScript

后端 未结 15 2577
不思量自难忘°
不思量自难忘° 2020-11-29 19:38

I have an object in JavaScript:

var obj = {
   "a": "test1",
   "b": "test2"
}

How do I check that te

15条回答
  •  旧时难觅i
    2020-11-29 20:05

    You can try this:

    function checkIfExistingValue(obj, key, value) {
        return obj.hasOwnProperty(key) && obj[key] === value;
    }
    var test = [{name : "jack", sex: F}, {name: "joe", sex: M}]
    console.log(test.some(function(person) { return checkIfExistingValue(person, "name", "jack"); }));
    

提交回复
热议问题