1

I have an array given with multiple objects as entries (collection) and I want to check if another source object is in those single entry objects. If so I want to return an array with all the objects that fulfill that condition. Here is the code with an example:

function whatIsInAName(collection, source) {
 var arr = [];
 var sourceEntries = Object.entries(source);
 for (var i = 0; i < collection.length; i++) {
 for (var j = 0; i < sourceEntries.length; i ++) {
 if((collection[i].hasOwnProperty(sourceEntries[j][0]))) {
 if(collection[i][sourceEntries[j][0]] == sourceEntries[j][1]) {
 /*what happens here*/
 }
 }
 arr.push(collection[i]);
 }
 }
 return arr;
}
print(whatIsInAName([{ "a": 1, "b": 2 }, { "a": 1 }, { "a": 1, "b": 2, "c": 2 }], { "a": 1, "b": 2 }));

Obviously I don't know what to put where it's written ("what happens here"). The problem is basically that the second for loop and both if conditions have to be true so the push command makes sense.

I appreciate any hint or help , thanks!

P.S. I know that this is probably not the most elegant way to solve it so happy about any other solutions as well.

Russ Cam
126k34 gold badges211 silver badges275 bronze badges
asked Jun 1, 2017 at 23:47
3
  • Can you give a sample input and output? I think that would help someone figure out exactly what you need. Commented Jun 1, 2017 at 23:50
  • I mean depending how thorough you really want to be, you could just test if JSON.stringify(item_in_array) == JSON.stringify(search_object) Commented Jun 2, 2017 at 0:04
  • Of course, @ArnavAggarwal ! So basically what it is in the print statement is the input, output should be: [{"a": 1, "b": 2}, {"a": 1, "b": 2, "c": 3}] because those two objects contain both 'a' and 'b'. Commented Jun 2, 2017 at 14:47

1 Answer 1

2

This is where the built-in .filter function comes in handy:

function whatIsInAName(collection, source) {
 return collection.filter((obj) => {
 for (var prop in source) {
 if (source[prop] !== obj[prop]) {
 // The source property is not found in obj - no good!
 return false;
 }
 // The source property matches one of the obj's properties - keep going!
 }
 // Made it through the checks! You've got a match!
 return true;
 });
}
console.log(whatIsInAName([{
 "a": 1,
 "b": 2
}, {
 "a": 1
}, {
 "a": 1,
 "b": 2,
 "c": 2
}], {
 "a": 1,
 "b": 2
}));

Or, if you're inclined to use a library to do this, it can be done very simply with Lodash:

var collection = [{ "a": 1, "b": 2 }, { "a": 1 }, { "a": 1, "b": 2, "c": 2 }],
 source = { "a": 1, "b": 2 };
console.log(_.filter(collection, source));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

answered Jun 1, 2017 at 23:55

1 Comment

Thanks, @j-titus Smooth and elegant way to solve it. Very much appreciated!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.