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

后端 未结 15 2481
不思量自难忘°
不思量自难忘° 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:19

    I did a test with all these examples, and I ran this in Node.js v8.11.2. Take this as a guide to select your best choice.

    let i, tt;
        const obj = { a: 'test1', b: 'test2', c: 'test3', d: 'test4', e: 'test5', f: 'test6' };
    
    console.time("test1")
    i = 0;
    for( ; i<1000000; i=i+1) {
      if (Object.values(obj).indexOf('test4') > -1) {
        tt = true;
      }
    }
    console.timeEnd("test1")
    
    console.time("test1.1")
    i = 0;
    for( ; i<1000000 ; i=i+1) {
      if (~Object.values(obj).indexOf('test4')) {
        tt = true;
      }
    }
    console.timeEnd("test1.1")
    
    console.time("test2")
    i = 0;
    for( ; i<1000000; i=i+1) {
      if (Object.values(obj).includes('test4')) {
        tt = true;
      }
    }
    console.timeEnd("test2")
    
    
    console.time("test3")
    i = 0;
    for( ; i<1000000 ; i=i+1) {
      for(const item in obj) {
        if(obj[item] == 'test4') {
          tt = true;
          break;
        }
      }
    }
    console.timeEnd("test3")
    
    console.time("test3.1")
    i = 0;
    for( ; i<1000000; i=i+1) {
      for(const [item, value] in obj) {
        if(value == 'test4') {
          tt = true;
          break;
        }
      }
    }
    console.timeEnd("test3.1")
    
    
    console.time("test4")
    i = 0;
    for( ; i<1000000; i=i+1) {
      tt = Object.values(obj).some((val, val2) => {
        return val == "test4"
      });
    }
    console.timeEnd("test4")
    
    console.time("test5")
    i = 0;
    for( ; i<1000000; i=i+1) {
      const arr = Object.keys(obj);
      const len = arr.length;
      let i2 = 0;
      for( ; i2<len ; i2=i2+1) {
        if(obj[arr[i2]] == "test4") {
          tt = true;
          break;
        }
      }
    }
    console.timeEnd("test5")

    Output on my server

    test1:   272.325 ms
    test1.1: 246.316 ms
    test2:   251.98 0ms
    test3:    73.284 ms
    test3.1: 102.029 ms
    test4:   339.299 ms
    test5:    85.527 ms
    
    0 讨论(0)
  • 2020-11-29 20:21

    You can turn the values of an Object into an array and test that a string is present. It assumes that the Object is not nested and the string is an exact match:

    var obj = { a: 'test1', b: 'test2' };
    if (Object.values(obj).indexOf('test1') > -1) {
       console.log('has test1');
    }
    

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values

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

    The simple answer to this is given below.

    This is working because every JavaScript type has a “constructor” property on it prototype”.

    let array = []
    array.constructor === Array
    // => true
    
    
    let data = {}
    data.constructor === Object
    // => true
    
    0 讨论(0)
提交回复
热议问题