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

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

I have an object in JavaScript:

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

How do I check that te

相关标签:
15条回答
  • 2020-11-29 20:01
    var obj = {"a": "test1", "b": "test2"};
    var getValuesOfObject = Object.values(obj)
    for(index = 0; index < getValuesOfObject.length; index++){
        return Boolean(getValuesOfObject[index] === "test1")
    }
    

    The Object.values() method returned an array (assigned to getValuesOfObject) containing the given object's (obj) own enumerable property values. The array was iterated using the for loop to retrieve each value (values in the getValuesfromObject) and returns a Boolean() function to find out if the expression ("text1" is a value in the looping array) is true.

    0 讨论(0)
  • 2020-11-29 20:03

    you can try this one

    var obj = {
      "a": "test1",
      "b": "test2"
    };
    
    const findSpecificStr = (obj, str) => {
      return Object.values(obj).includes(str);
    }
    
    findSpecificStr(obj, 'test1');
    
    0 讨论(0)
  • 2020-11-29 20:04
    getValue = function (object, key) {
        return key.split(".").reduce(function (obj, val) {
            return (typeof obj == "undefined" || obj === null || obj === "") ? obj : (_.isString(obj[val]) ? obj[val].trim() : obj[val]);}, object);
    };
    
    var obj = {
       "a": "test1",
       "b": "test2"
    };
    

    Function called:

     getValue(obj, "a");
    
    0 讨论(0)
  • 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"); }));
    
    0 讨论(0)
  • 2020-11-29 20:06

    You can use Object.values():

    The Object.values() method returns an array of a given object's own enumerable property values, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).

    and then use the indexOf() method:

    The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

    For example:

    Object.values(obj).indexOf("test`") >= 0
    

    A more verbose example is below:

    var obj = {
      "a": "test1",
      "b": "test2"
    }
    
    
    console.log(Object.values(obj).indexOf("test1")); // 0
    console.log(Object.values(obj).indexOf("test2")); // 1
    
    console.log(Object.values(obj).indexOf("test1") >= 0); // true
    console.log(Object.values(obj).indexOf("test2") >= 0); // true 
    
    console.log(Object.values(obj).indexOf("test10")); // -1
    console.log(Object.values(obj).indexOf("test10") >= 0); // false

    0 讨论(0)
  • 2020-11-29 20:08

    This should be a simple check.

    Example 1

    var myObj = {"a": "test1"}
    
    if(myObj.a == "test1") {
        alert("test1 exists!");
    }
    
    0 讨论(0)
提交回复
热议问题