1

I am trying to get a new array object based on two of them.

log1 = {1:"London",2:"New york",4:"Berlin"};
log2 = [{id:1, location:"EU"},{id:2, location:"US"},{id:18, location:"Asia"}];

I want to make sure that log1 keys all exist in log2 and, if not, I want to delete it. So for this will be:

 result = {1:"London",2:"New york"};

I couldn't do it with filter as it take only one object. Is there any way to filter based on two object arrays? Is there any neat way to do it ?

2
  • delete from log1 or log2? Commented Apr 24, 2014 at 10:47
  • delete from log1. So log2 is fixed and log1 should change every time log2 dropping an id. Commented Apr 24, 2014 at 10:49

3 Answers 3

3

If you are using a recent version of JS with Array.map(), Array.filter() and Object.keys() you could try the following for a slightly more functional approach:

var log1 = {
    1: "London",
    2: "New york",
    4: "Berlin"
};

var log2 = [
    {id: 1,  location: "EU"},
    {id: 2,  location: "US"},
    {id: 18, location: "Asia"}
];


var ids = log2.map(function(obj) { return obj.id; }).map(String);

Object.keys(log1).filter(function(key) {
    return ids.indexOf(key) === -1;
}).forEach(function(k) {
    delete log1[k];
});

console.log(log1);

Should yield:

{ '1': 'London', '2': 'New york' }

Example: http://repl.it/RnF/2

Hope this helps :)

Edit

Otherwise, for browser compatibility and much more you could consider lodash or underscore as suggested by other posters. Or use a polyfill!

MDN provides polyfills at the bottom of each of the following pages for older environments (in case you require the compatibility):

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

6 Comments

Note: the line return '' + obj.id; is just a shortcut to cast the id values from log2 from number to string because Object.keys() will return numeric keys from log1 as strings.
I'd probably do log2.map(function(obj){ return obj.id }).map(String)
Ah, of course. That's definitely more succinct - I like that. I'll update my answer to reflect that. Cheers!
even if this looks so nice i prefer the old way as the performance is much higher...jsperf.com/old-vs-new-obj-arr-while anyway nice piece of code
@cocco that's a really broken benchmark, moreover, it is irrelevant for under thousands of times and thousands of items.
|
3

Here is how you can do it in pure js:

    log1 = {1:"London",2:"New york",4:"Berlin"};
    log2 = [{id:1, location:"EU"},{id:2, location:"US"},{id:18, location:"Asia"}];

    for (var prop1 in log1) {
        var found = false;
        for (var i = 0; i < log2.length; i++) {
            if (log2[i].id.toString() === prop1) {
                found = true;
                break;
            }
        }
        if (!found) {
            delete log1[prop1]
        }
    }

Comments

0

Use http://underscorejs.org/

It has pluck,filter and map functions that can help you out.. Its quite powerful..

2 Comments

Native JavaScript has .map and .filter too, and .pluck is just a .map .
Checkout lodash.com it's much faster than underscorejs and has the same functionality (+ much more!).

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.