检查对象在javascript中是否有一组属性

检查对象在javascript中是否有一组属性,javascript,jquery,object,properties,operators,Javascript,Jquery,Object,Properties,Operators,假设我有一个名为a的对象,我如何检查a是否有一个特定的多属性列表,我认为可以使用中的逻辑运算符来完成 类似这样的内容: var a = {prop1:{},prop2:{},prop3:{}}; if ({1:"prop1",2:"prop2",3:"prop3"} in a) console.log("a has these properties:'prop1, prop2 and prop3'"); 编辑 如果普通javascript帮不上忙,jQuery也可以,但我更喜欢java

假设我有一个名为
a
的对象,我如何检查
a
是否有一个特定的多属性列表,我认为可以使用中的逻辑运算符来完成

类似这样的内容:

var a = {prop1:{},prop2:{},prop3:{}};
if ({1:"prop1",2:"prop2",3:"prop3"} in a)
    console.log("a has these properties:'prop1, prop2 and prop3'");
编辑

如果普通javascript帮不上忙,jQuery也可以,但我更喜欢javascript

EDIT2


可移植性是特权

最简单的方法是使用传统的
&&

if ("prop1" in a && "prop2" in a && "prop3" in a) 
    console.log("a has these properties:'prop1, prop2 and prop3'");
这不是一个“速记”,但它并不比你所提议的长多少

您还可以将要测试的属性名称放置在数组中,并使用以下方法:

但是请注意,这是在ECMAScript 5中引入的,因此在一些较旧的浏览器上不可用。如果这是一个问题,您可以提供自己的版本。以下是MDN的实现:

if (!Array.prototype.every) {
  Array.prototype.every = function(fun /*, thisp */) {
    'use strict';
    var t, len, i, thisp;

    if (this == null) {
      throw new TypeError();
    }

    t = Object(this);
    len = t.length >>> 0;
    if (typeof fun !== 'function') {
        throw new TypeError();
    }

    thisp = arguments[1];
    for (i = 0; i < len; i++) {
      if (i in t && !fun.call(thisp, t[i], i, t)) {
        return false;
      }
    }

    return true;
  };
}
if(!Array.prototype.every){
Array.prototype.every=函数(fun/*,thisp*/){
"严格使用",;
变量t,len,i,thisp;
if(this==null){
抛出新的TypeError();
}
t=对象(本);
len=t.length>>>0;
如果(乐趣的类型!=“功能”){
抛出新的TypeError();
}
thisp=参数[1];
对于(i=0;i
像这样:

var testProps = ['prop1', 'prop2', 'prop3', 'prop4'];
var num = -1, outA;
for(var i in a){
  if(i === testProps[++num])outA[num] = i;
}
console.log('properties in a: '+outA.join(', '));
这样使用:

var testProps = ['prop1', 'prop2', 'prop3'];
var a = {prop1:{},prop2:{},prop3:{}};

var result = testProps.reduce(function(i,j) { return i && j in a }, true);
console.log(result);
这才是真正闪耀的地方。例如,它提供了一个已填充的
every()
方法,正如对p.s.w.g.答案的评论中所建议的:

但有不止一种方法可以做到这一点;下面的内容虽然更详细,但也可能适合您的需要,并让您了解下划线可以做的更多事情(例如.key和.intersection)


我认为尝试一下很好:

/* Create an object class */
var obj = function(){ this.attributes = new Array(); }
obj.prototype.addProp = function(value){ this.attributes.push(new attribute(value)); }
obj.prototype.hasProp = function(value){ 
    for(var i = 0; i < this.attributes.length; i++){ 
      if(value == this.attributes[i].value) return true; } return false; }
function attribute(value){
    this.value = value;
}
/* Testing object has some value*/
var ob = new obj();
ob.addProp('1');
ob.addProp('2');
ob.addProp('3');
 //* check value index
//alert(ob.attributes[0].value);
 //* check if ob has prop
alert(ob.hasProp('1'));
/*创建一个对象类*/
var obj=function(){this.attributes=new Array();}
obj.prototype.addProp=函数(值){this.attributes.push(新属性(值));}
obj.prototype.hasProp=函数(值){
对于(var i=0;i

下面是
对象更优雅的用法。要包含
try catch
的each()
原型函数:

try {
  const required = ['prop1', 'prop2', 'prop3']
  const data = {prop1: 'hello', prop2: 'world', prop3: 'bad'}
  if (!required.every( x => x in data )) throw new Error('missing property')
  console.log('all properties found')
} catch(err) {
  console.log(err.message)
}
``

我相信它是有效的,现在,有没有更优雅的解决方案如果我们的属性列表很长,那么传统的方法就太长了,第二种解决方案看起来不错,让我们看看,每个解决方案都需要jQuery吗?@Benedictus不,这是vanilla JS。对不起,如果它在旧浏览器上不起作用,那就不好了:)我想我必须坚持(b中的a)如果没有更顺利的事情发生out@Paul请注意,
如果任何元素返回
false
,则每个
也会提前退出,因此它在功能上与
某些
相同,只是布尔值颠倒了。如果对其进行多填充,则它与另一个答案相同,而另一个答案的信息量要大得多,很抱歉要删除答案:)下划线.js为您进行多边形填充(为了使用旧浏览器),并且是一个库,可以通过使用它使您成为更好的代码;例如,考虑一本书“功能JavaScript”(和免费/取样器PDF):
/* Create an object class */
var obj = function(){ this.attributes = new Array(); }
obj.prototype.addProp = function(value){ this.attributes.push(new attribute(value)); }
obj.prototype.hasProp = function(value){ 
    for(var i = 0; i < this.attributes.length; i++){ 
      if(value == this.attributes[i].value) return true; } return false; }
function attribute(value){
    this.value = value;
}
/* Testing object has some value*/
var ob = new obj();
ob.addProp('1');
ob.addProp('2');
ob.addProp('3');
 //* check value index
//alert(ob.attributes[0].value);
 //* check if ob has prop
alert(ob.hasProp('1'));
try {
  const required = ['prop1', 'prop2', 'prop3']
  const data = {prop1: 'hello', prop2: 'world', prop3: 'bad'}
  if (!required.every( x => x in data )) throw new Error('missing property')
  console.log('all properties found')
} catch(err) {
  console.log(err.message)
}
``