如何使用javascript检查对象中是否存在值 [英] How to check if value exists in object using javascript

查看:110
本文介绍了如何使用javascript检查对象中是否存在值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在javascript中有一个对象

I have an object in javascript

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

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

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

推荐答案

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

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

这篇关于如何使用javascript检查对象中是否存在值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆