0

enter image description here

...but with objects.

for example :

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

result = somethingJavascriptHas.ObjectAnd(one, two);

console.log(result);

would return :

{a: false, c: false};

(I'm interested in the key, not the rest, so I discard the second object's value in favor of the first's here. In my case the two object's values will always be the same simply some of their entries will be missing or not and I want a final object that includes only key's (with the first object key's value) that are present in both objects)

is this possible?

For completeness' sake OR, XOR and NOT. (again assume priority of values for the first passed object)

3
  • 2
    Yes it's possible. You'd just have to write some code to do it. Commented Mar 2, 2018 at 14:59
  • not somtheing Object.something or lodash would already have then? Commented Mar 2, 2018 at 15:02
  • 1
    @tatsu Asking for library suggestions is one of the reasons for closing questions at Stack Overflow Commented Mar 2, 2018 at 15:23

4 Answers 4

2

You could use the Array#reduce function on one of your objects keys and check if the properties of both objects are not undefined :

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

Sign up to request clarification or add additional context in comments.

Comments

2

just loop through your objects:

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)

Comments

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

Comments

0

Since you said you wanted just keys, you can combine 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));

1 Comment

okay I'll admit my wording was misleading but to me "not being interested in" doesn't by definition promote itself to "time to delete". I should have said "I'm interested in the key, not the rest, for this part" or "for now". the distinction being that the second array's object's were not being taken into consideration by the operation. interesting possibility though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.