0

assuming we have two arrays like that :

var array1 = [
    {"name":"foo", "age": 13},
    {"name":"bar", "age": 15},
    {"name":"foobar", "age": 9},

    {"name":"i", "age": 60},
    {"name":"want", "age": 41},
    {"name":"to", "age": 31},
    {"name":"extract", "age": 2},
    {"name":"all", "age": 34},
    {"name":"these", "age": 54}
];

var array2 = [
    {"name":"foo", "age": 13},
    {"name":"bar", "age": 15},
    {"name":"foobar", "age": 9}
];

I would like to extract from array1 all objects i have not in array2 using the key name as reference. So at the end of the treatment, I would like to have the array :

var expectedResults = [
    {"name":"i", "age": 60},
    {"name":"want", "age": 41},
    {"name":"to", "age": 31},
    {"name":"extract", "age": 2},
    {"name":"all", "age": 34},
    {"name":"these", "age": 54}
}

Any idea to do that ? I can't find a method in JS which seems to allow that kind of filter.

7
  • 1
    Just loop through the initial array, and keep the ones you need. Commented Jul 10, 2014 at 14:32
  • I hoped there is more elegant solution =/ Commented Jul 10, 2014 at 14:37
  • " I can't find a method in JS which seems to allow that kind of filter." You know... sometimes you actually need to write a little code. Commented Jul 10, 2014 at 14:39
  • @kl94: It's the simplest thing that could work. I suggest you don't try to do premature optimization, and just get it to work first. From your question I got the impression you hadn't even managed to get it to work yet. Commented Jul 10, 2014 at 14:39
  • @cookiemonster Agree, but in some others languages. You can do it easily Commented Jul 10, 2014 at 14:41

1 Answer 1

4

You can get all the names from array2 like this

var names = array2.map(function(currentObject) {
    return currentObject.name;
});

And filter objects from array1, based on the gathered names like this

var result = array1.filter(function(currentObject) {
    return names.indexOf(currentObject.name) === -1;
});

console.log(result);

Output

[ { name: 'i', age: 60 },
  { name: 'want', age: 41 },
  { name: 'to', age: 31 },
  { name: 'extract', age: 2 },
  { name: 'all', age: 34 },
  { name: 'these', age: 54 } ]

As you want to make the code a little shorter, you can do something like this

var result = array1.filter(function(currentObject) {
    return array2.every(function(currentArray2Object) {
        return currentArray2Object.name !== currentObject.name;
    })
});

But if the arrays are big, then the efficient solution would be

var names = array2.reduce(function(result, currentObject) {
    result[currentObject.name] = currentObject.age;
    return result;
}, {});

var result = array1.filter(function(currentObject) {
    return !names.hasOwnProperty(currentObject.name);
});

console.log(result);
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your answer, it did the work done. But I really thought there was a better way to do kind of work. Thanks again.
@kl94 By better, you mean shorter or efficient?
I was looking for a shorter solution, but I think we just can't. Once again, thanks you.
@kl94 I just included an alternate version, please check.
@BenjaminAnderson Thanks :-) I also included an efficient version, please check.
|

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.