0

I have code like this :

// inq is array of id
var inq = ["002216", "000018", "000197"];
var filterExt = { where: { id: { inq: inq } };

Radio.find(filterExt, function(err, results){
  if(err) cb(err);
  else {
      // results's index is always following "id" order
      cb(null, results);
  }
});

My expected results is the array of results are have same order like "inq". But the results order always following "id" order. Is there any way to get same results order with inq order?

Thanks.

1 Answers1

0

Mark the result by the identifier, then use it in the loop. Example with library "lodash" (mapping by key).

var __ = require('lodash');

// inq is array of id
var inq = ["002216", "000018", "000197"];
var filterExt = {
    where: {
        id: {
            inq: inq
        }
    }
};

Radio.find(filterExt, function(err, results) {
    if (err) cb(err);
    else {
        var map = __.keyBy(results, "id");
        for(var id in inq) {
            var row = map[id];
        }

        cb(null, results);
    }
});
Denis Kulygin
  • 323
  • 1
  • 9