如何确定Javascript对象是否只有一个特定的键值对? [英] How to determine if a Javascript object has only one specific key-value pair?
本文介绍了如何确定Javascript对象是否只有一个特定的键值对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
确定Javascript对象是否只有一个特定键值对的最简单方法是什么?
What would be the easiest way to determine if a Javascript object has only one specific key-value pair?
例如,我需要确保该对象存储在变量 text
中的只包含键值对'id':'message'
For example, I need to make sure that the object stored in the variable text
only contains the key-value pair 'id' : 'message'
推荐答案
var keys = Object.keys(text), key = keys[0];
if (keys.length !== 1 || key !== "id" || text[key] !== "message")
alert("Wrong object");
这篇关于如何确定Javascript对象是否只有一个特定的键值对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文