# 使用对象的JS布尔逻辑运算

##### 问题描述投票：0回答：4

......但是有了物体。

``````one = {a: false, c: false, e: false};
two = {a: true, b: false, c: false, d:false};

result = somethingJavascriptHas.ObjectAnd(one, two);

console.log(result);
``````

``````{a: false, c: false};
``````

（我对密钥感兴趣，而不是其余部分，所以我放弃了第二个对象的值，而不是第一个在这里。在我的情况下，两个对象的值总是相同的，只是它们的一些条目将丢失或没有。我想要一个最终对象，其中只包含两个对象中存在的键（具有第一个对象键的值）

javascript object boolean-logic boolean-operations
##### 4个回答
2

``````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);``````

2

``````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)``````

0

``````// 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);``````

0

``````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));``````