如何使用javascript检查对象中是否存在值

时间:2022-06-01 23:44:36

I have an object in javascript

我在javascript中有一个对象

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

How do I check that test1 exists in the object as a value?

如何检查对象中是否存在test1作为值?

9 个解决方案

#1


57  

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:

您可以将Object的值转换为数组并测试字符串是否存在。它假定Object不是嵌套的,并且字符串是完全匹配的:

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

#2


16  

You can use the Array method .some:

您可以使用Array方法.some:

var exists = Object.keys(obj).some(function(k) {
    return obj[k] === "test1";
});

#3


16  

try:

var obj = {
   "a": "test1",
   "b": "test2"
};
Object.keys(obj).forEach(function(key) {
  if (obj[key] == 'test1') {
    alert('exists');
  }
});

or

var obj = {
   "a": "test1",
   "b": "test2"
};
var found = Object.keys(obj).filter(function(key) {
  return obj[key] === 'test1';
});
if (found.length) {
   alert('exists');
}

UPDATE this will not work for NaN and -0 for those values, you can use (instead of ===) new in ES6:

更新这不适用于NaN和-0这些值,您可以在ES6中使用(而不是===)new:

 Object.is(obj[key], value);

#4


5  

Use for...in loop:

用于... in循环:

for (const k in obj) {
    if (obj[k] === "test1") {
        return true;
    }
}

#5


2  

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"); }));

#6


1  

I did a test with this all examples, I ran this in nodejs v8.11.2, take this as guide to select your best choice.

我用这个例子做了一个测试,我在nodejs v8.11.2中运行它,以此为指导选择你的最佳选择。

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 = obj[arr[i2]];
    			break;
    		}
    	}
    }
    console.timeEnd("test5")

Output in my server

在我的服务器输出

test1: 272.325ms
test1.1: 246.316ms
test2: 251.980ms
test3: 73.284ms
test3.1: 102.029ms
test4: 339.299ms
test5: 85.527ms

#7


0  

This will do the trick.

这样就可以了。

if(value in object){
   return true;
 } 

#8


-3  

if( myObj.hasOwnProperty('key') && myObj['key'] === value ){
    ...
}

#9


-5  

This should be a simple check.

这应该是一个简单的检查。

Example 1

   var myObj = {"a": "test1"}

if(myObj.a == "test1") {
    alert("test1 exists!");
}

#1


57  

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:

您可以将Object的值转换为数组并测试字符串是否存在。它假定Object不是嵌套的,并且字符串是完全匹配的:

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

#2


16  

You can use the Array method .some:

您可以使用Array方法.some:

var exists = Object.keys(obj).some(function(k) {
    return obj[k] === "test1";
});

#3


16  

try:

var obj = {
   "a": "test1",
   "b": "test2"
};
Object.keys(obj).forEach(function(key) {
  if (obj[key] == 'test1') {
    alert('exists');
  }
});

or

var obj = {
   "a": "test1",
   "b": "test2"
};
var found = Object.keys(obj).filter(function(key) {
  return obj[key] === 'test1';
});
if (found.length) {
   alert('exists');
}

UPDATE this will not work for NaN and -0 for those values, you can use (instead of ===) new in ES6:

更新这不适用于NaN和-0这些值,您可以在ES6中使用(而不是===)new:

 Object.is(obj[key], value);

#4


5  

Use for...in loop:

用于... in循环:

for (const k in obj) {
    if (obj[k] === "test1") {
        return true;
    }
}

#5


2  

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"); }));

#6


1  

I did a test with this all examples, I ran this in nodejs v8.11.2, take this as guide to select your best choice.

我用这个例子做了一个测试,我在nodejs v8.11.2中运行它,以此为指导选择你的最佳选择。

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 = obj[arr[i2]];
    			break;
    		}
    	}
    }
    console.timeEnd("test5")

Output in my server

在我的服务器输出

test1: 272.325ms
test1.1: 246.316ms
test2: 251.980ms
test3: 73.284ms
test3.1: 102.029ms
test4: 339.299ms
test5: 85.527ms

#7


0  

This will do the trick.

这样就可以了。

if(value in object){
   return true;
 } 

#8


-3  

if( myObj.hasOwnProperty('key') && myObj['key'] === value ){
    ...
}

#9


-5  

This should be a simple check.

这应该是一个简单的检查。

Example 1

   var myObj = {"a": "test1"}

if(myObj.a == "test1") {
    alert("test1 exists!");
}