JS 对象的布尔逻辑运算
JS Boolean Logic Operation With Object
...但是有对象。
例如:
one = {a: false, c: false, e: false};
two = {a: true, b: false, c: false, d:false};
result = somethingJavascriptHas.ObjectAnd(one, two);
console.log(result);
会 return :
{a: false, c: false};
(我对键感兴趣,而不是其余的,所以我在这里放弃了第二个对象的值以支持第一个对象。在我的例子中,两个对象的值将始终相同,只是它们的一些条目将是否丢失,我想要一个最终对象,它只包含两个对象中都存在的键(具有第一个对象键的值))
这可能吗?
为了完整起见,OR、XOR 和 NOT。 (再次假定第一个传递的对象的值的优先级)
只需遍历您的对象:
var one = {a: false, c: false, e: false};
var two = {a: true, b: false, c: false, d:false};
var res = {}
for (var o in one) {
for (var t in two) {
if (t == o) res[t] = one[t]
}
}
console.log(res)
您可以在您的对象键之一上使用 Array#reduce
函数并检查两个对象的属性是否未定义:
var one = {a: false, c: false, e: false};
var two = {a: true, b: false, c: false, d:false};
var three = Object.keys(one).reduce((acc, curr) => {
if(two[curr] !== undefined){
acc[curr] = one[curr] && two[curr];
}
return acc;
}, {});
console.log(three);
// Your example:
/*one = {a: false, c: false, e: false};
two = {a: true, b: false, c: false, d:false};
result = somethingJavascriptHas.ObjectAnd(a, b);
console.log(result);
// would return :
{a: false, c: false};*/
// Working code:
const one = {a: false, c: false, e: false};
const two = {a: true, b: false, c: false, d:false};
const oneTwoIntersection = Object.keys(one)
.reduce((currentObj, nextKey) => {
return !two.hasOwnProperty(nextKey) ? currentObj : Object.assign({}, currentObj, {
[nextKey]: one[nextKey]
});
}, {});
console.log(oneTwoIntersection);
既然你说你只想要钥匙,你可以结合Object.keys, Array#filter and Object#hasOwnProperty
const one = {a: false, c: false, e: false};
const two = {a: true, b: false, c: false, d:false};
const intersection = (a, b) =>
Object.keys(a).filter(aKey => b.hasOwnProperty(aKey));
console.log(intersection(one, two));
...但是有对象。
例如:
one = {a: false, c: false, e: false};
two = {a: true, b: false, c: false, d:false};
result = somethingJavascriptHas.ObjectAnd(one, two);
console.log(result);
会 return :
{a: false, c: false};
(我对键感兴趣,而不是其余的,所以我在这里放弃了第二个对象的值以支持第一个对象。在我的例子中,两个对象的值将始终相同,只是它们的一些条目将是否丢失,我想要一个最终对象,它只包含两个对象中都存在的键(具有第一个对象键的值))
这可能吗?
为了完整起见,OR、XOR 和 NOT。 (再次假定第一个传递的对象的值的优先级)
只需遍历您的对象:
var one = {a: false, c: false, e: false};
var two = {a: true, b: false, c: false, d:false};
var res = {}
for (var o in one) {
for (var t in two) {
if (t == o) res[t] = one[t]
}
}
console.log(res)
您可以在您的对象键之一上使用 Array#reduce
函数并检查两个对象的属性是否未定义:
var one = {a: false, c: false, e: false};
var two = {a: true, b: false, c: false, d:false};
var three = Object.keys(one).reduce((acc, curr) => {
if(two[curr] !== undefined){
acc[curr] = one[curr] && two[curr];
}
return acc;
}, {});
console.log(three);
// Your example:
/*one = {a: false, c: false, e: false};
two = {a: true, b: false, c: false, d:false};
result = somethingJavascriptHas.ObjectAnd(a, b);
console.log(result);
// would return :
{a: false, c: false};*/
// Working code:
const one = {a: false, c: false, e: false};
const two = {a: true, b: false, c: false, d:false};
const oneTwoIntersection = Object.keys(one)
.reduce((currentObj, nextKey) => {
return !two.hasOwnProperty(nextKey) ? currentObj : Object.assign({}, currentObj, {
[nextKey]: one[nextKey]
});
}, {});
console.log(oneTwoIntersection);
既然你说你只想要钥匙,你可以结合Object.keys, Array#filter and Object#hasOwnProperty
const one = {a: false, c: false, e: false};
const two = {a: true, b: false, c: false, d:false};
const intersection = (a, b) =>
Object.keys(a).filter(aKey => b.hasOwnProperty(aKey));
console.log(intersection(one, two));