0

I have an array of models where I have the type and key.

models = [{type: 'user', key: getKey('user')},    
    //.
    //.Many more model objects here
    //.
    {type: 'employee',  key: getKey('employee')}];

Here I'm mapping each of the model to my search function which gets data for each model:

async.map(models, search, function (err, results) {
    if (err) {
        res.json(400, err);
    } else {
        // Here results is an array of arrays of different results for different models and I dont know how to check
        // how to find different models with the same key and union their results if they have the same key value...
        var mergedResult = [];
        mergedResult = mergedResult.concat.apply(mergedResult, results);
        res.json(200, mergedResult);
    }
});

And Here is my search function which returns for passed model name:

function search(model, callback) {
    getData(model.type, callback);
}

So now I'm getting the result for each model (user, customer,... and employee), but now I want to check if any of these models have the same key which I get by getKey(modelName) function; in our example lets say the key for user and employee is the same and it is "name" field, so in this case I want to union (like the union in sql) the results of employee and user: lets say for "user" we get:

Results for user model:

[
    {
        name : "Mike",
        age: 33
        eyeColor: "brown"
    },
    {
        name : "David",
        age: 35
    }
]

and for employee we get :

Results for Employee model:

    [
        {
            name : "Mike",
            age: 33,
            heigh: 178,
            eyeColor: "black"
        },
        {
            name : "Nanacy",
            age: 39
        }
    ]     

The desired results:

[
    {
        name : "Mike",
        age: 33
        eyeColor: "brown",
        heigh: 178
    },
    {
        name : "David",
        age: 35
    },
    {
        name : "Nanacy",
        age: 39
    }
]

In my algorithm I'm just simply returning:

What I'm returning:

[
    {
        name : "Mike",
        age: 33
        eyeColor: "brown"
    },
    {
        name : "David",
        age: 35
    },
    {
        name : "Mike",
        age: 33,
        heigh: 178,
        eyeColor: "black"
    },
    {
        name : "Nanacy",
        age: 39
    }
]       

Please let me know if I you need more calrification! Thanks

10
  • 1
    Do have any actual problems with async? Could you have solved the problem if the functions were synchronous? If so, please show us that (pseudo-)code, it might be easier to understand than your text. Commented Dec 11, 2014 at 4:10
  • Map through Object.keys/[].keys() instead Commented Dec 11, 2014 at 4:45
  • @Bergi Thanks man! I guess I simplified my question; I hope this makes more sense; I'm just looking for the same functionality as union of mysql; my search function is giving back the results for each model... Commented Dec 11, 2014 at 4:58
  • @vp_arth Thanks man! but in this case Object.keys would be just the index number?! Could you please elaborate your solution... Commented Dec 11, 2014 at 4:59
  • async.map retains the order of the items. results[0] will be the result of getData(models[0].type) and so on. With that information, you can simply loop the models to determine in what order which result sets should be merged on what key, and then just reference each result set by its index. Your "union" algorithm is a bit underspecified though, so you'll have to figure that out yourself (e.g. how it would deal with the data if Mike's age would be different in the two results). Commented Dec 11, 2014 at 5:07

1 Answer 1

1

You should save key about info, while you have it:

function search(model, next) {
  var data = getData(model.type, function(err, data){
     if (err) return next(err);
     data['__key'] = data[model.key];
     next(null, data)
  });
}
Sign up to request clarification or add additional context in comments.

Comments

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.