Javascript 如何使用javascript检查对象中是否存在值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35948669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if value exists in object using javascript
提问by Brown KL
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 是否作为值存在于对象中?
回答by Matt Pileggi
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
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values
回答by tymeJV
You can use the Array method .some
:
您可以使用数组方法.some
:
var exists = Object.keys(obj).some(function(k) {
return obj[k] === "test1";
});
回答by jcubic
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');
}
UPDATEthis will not work for NaN
and -0
for those values, you can use (instead of ===
) new in ES6:
UPDATE这不会工作NaN
,并-0
为这些值,你可以使用(而不是===
在ES6)新:
Object.is(obj[key], value);
UPDATE
更新
With modern browsers you can also use:
使用现代浏览器,您还可以使用:
var obj = {
"a": "test1",
"b": "test2"
};
if (Object.values(obj).includes('test1')) {
alert('exists');
}
回答by isvforall
回答by Dangerousmouse
You can use Object.values():
您可以使用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 afor...in
loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).
该
Object.values()
方法返回给定对象自己的可枚举属性值的数组,其顺序与for...in
循环提供的顺序相同 (区别在于 for-in 循环也枚举原型链中的属性)。
and then use the indexOf()method:
然后使用indexOf()方法:
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.
该
indexOf()
方法返回可以在数组中找到给定元素的第一个索引,如果不存在,则返回 -1。
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
回答by chickens
Shortest ES6+ one liner:
最短的 ES6+ 单线:
let exists = Object.values(obj).includes("test1");
回答by DarckBlezzer
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 = true;
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
回答by Joshua Shibu
for one liner I would say
对于一个班轮我会说
exist=Object.values(obj).includes("test1");
console.log(exist);
回答by Yokki vallayok
You can try this:
你可以试试这个:
function checkIfExistingValue(obj, key, value) {
return obj.hasOwnProperty(key) && obj[key] === value;
}
var test = [{name : "Hyman", sex: F}, {name: "joe", sex: M}]
console.log(test.some(function(person) { return checkIfExistingValue(person, "name", "Hyman"); }));
回答by Devang Hire
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");