admin管理员组文章数量:1401384
I have two JS objects, I want to check if the first Object has all the second Object's keys and do something, otherwise, throw an exception. What's the best way to do it?
function(obj1, obj2){
if(obj1.HasAllKeys(obj2)) {
//do something
}
else{
throw new Error(...);
}
};
For example in below example since FirstObject has all the SecondObject's key it should run the code :
FirstObject
{
a : '....',
b : '...',
c : '...',
d : '...'
}
SecondObject
{
b : '...',
d : '...'
}
But in below example I want to throw an exception since XXX doesnt exist in FirstObject:
FirstObject
{
a : '....',
b : '...',
c : '...',
d : '...'
}
SecondObject
{
b : '...',
XXX : '...'
}
I have two JS objects, I want to check if the first Object has all the second Object's keys and do something, otherwise, throw an exception. What's the best way to do it?
function(obj1, obj2){
if(obj1.HasAllKeys(obj2)) {
//do something
}
else{
throw new Error(...);
}
};
For example in below example since FirstObject has all the SecondObject's key it should run the code :
FirstObject
{
a : '....',
b : '...',
c : '...',
d : '...'
}
SecondObject
{
b : '...',
d : '...'
}
But in below example I want to throw an exception since XXX doesnt exist in FirstObject:
FirstObject
{
a : '....',
b : '...',
c : '...',
d : '...'
}
SecondObject
{
b : '...',
XXX : '...'
}
Share
Improve this question
edited Nov 6, 2018 at 15:44
Am1rr3zA
asked Nov 19, 2015 at 21:28
Am1rr3zAAm1rr3zA
7,44118 gold badges87 silver badges134 bronze badges
4 Answers
Reset to default 5You can use:
var hasAll = Object.keys(obj1).every(function(key) {
return Object.prototype.hasOwnProperty.call(obj2, key);
});
console.log(hasAll); // true if obj2 has all - but maybe more - keys that obj1 have.
As a "one-liner":
var hasAll = Object.keys(obj1).every(Object.prototype.hasOwnProperty.bind(obj2));
You can write a function to iterate and check:
function hasAllKeys(requiredObj, secondObj) {
for (var key in requiredObj) {
if (!secondObj.hasOwnProperty(key)) {
return false;
}
}
return true;
}
hasAllKeys(SecondObject, FirstObject);
You can use jQuery's $.map
method as follows:
$.map(a,function(value, key) {return b[key];}).length != b.length
Another way to do this, that seems to work fine as well.
const o1 = {
a : '....',
b : '...',
c : '...',
d : '...'
},
o2 = {
b : '...',
d : '...',
};
///////////////////////
///// I. IN ONE LINE
///////////////////////
/**/
/**/ if(Object.keys(o2).every(key =>
/**/ key in o1
/**/ // Object.keys(o1).includes(key)
/**/ // Object.keys(o1).filter(x => x==key).length
/**/ ))
/**/ console.log(true);
/**/ else throw new Error("...");
/**/
///////////////////////
///////////////////////
///// II. IN A FUNCTION
///////////////////////
/**/
/**/ var hasAll = (o1 , o2) => {
/**/ let r = Object.keys(o2).every(key =>
/**/ // Object.keys(o1).includes(key)
/**/ // key in o1
/**/ // Object.keys(o1).filter(x => x==key).length
/**/
/**/ Object.keys(o1)
/**/ .reduce((acc,act,arr) =>
/**/ acc && (key in o1) // , true
/**/ )
/**/
/**/ );
/**/ return r;
/**/ }
/**/
/**/ let cond = hasAll(o1 , o2);
/**/
/**/ console.log(cond);
/**/
/**/ if (cond) console.log(true)
// /**/ else throw new Error("At least, one key doesn'e exist");
/**/
/**/ ////////
/**/
/**/ cond = hasAll(o2 , o1);
/**/
/**/ console.log(cond);
/**/
/**/ if (cond) console.log(true)
/**/ else throw new Error("At least, one key doesn'e exist");
/**/
///////////////////////
本文标签: Best way to Check a JavaScript Object has all the keys of another JavaScript ObjectStack Overflow
版权声明:本文标题:Best way to Check a JavaScript Object has all the keys of another JavaScript Object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744247798a2597105.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论