访问对象并使用多个条件进行排序

时间:2017-09-29 10:44:27

标签: javascript

ARRAYS:使用多个条件在JavaScript中访问对象和排序。 我是javascript的新手所以它似乎是非常基本的问题。 我必须根据状态字段排序..

var issues = cf.getEventMessage(reply); //issues has all the data
var assigneeTasks = {};
for(var i=0;i<issues.length ;i++){
          var record = issues[i];
          if(assigneeTasks[record.assigneemail] == undefined){
            assigneeTasks[record.assigneemail] = [];
          }
            assigneeTasks[record.assigneemail].push(record); //sorted according to assigneemail
        }

现在assigneeTasks

{"dev@vtu.com":
[
{"id":"T-728","assignedTo":"dev","assigneemail":"dev@inchfactory.com","duedate":"2017-09-29","status":"Open"},
{"id":"T-727","assignedTo":"dev","assigneemail":"dev@inchfactory.com","duedate":"2017-09-29","status":"In Progress"},
{"id":"T-726","assignedTo":"dev","assigneemail":"dev@inchfactory.com","duedate":"2017-10-04","status":"Open"},
{"id":"T-679","assignedTo":"devt","assigneemail":"dev@inchfactory.com","duedate":"2017-09-29","status":"Under Review"},
{"id":"T-645","assignedTo":"dev","assigneemail":"dev@inchfactory.com","duedate":"2017-09-27","status":"In Progress"}
],


"paul@vtu.com":
[
{"id":"T-728","assignedTo":"paul","assigneemail":"paul@inchfactory.com","duedate":"2017-09-29","status":"Open"},
{"id":"T-727","assignedTo":"paul","assigneemail":"paul@inchfactory.com","duedate":"2017-09-29","status":"In Progress"},
{"id":"T-726","assignedTo":"paul","assigneemail":"paul@inchfactory.com","duedate":"2017-10-04","status":"Open"},
{"id":"T-679","assignedTo":"paul","assigneemail":"paul@inchfactory.com","duedate":"2017-09-29","status":"Under Review"},
{"id":"T-645","assignedTo":"paul","assigneemail":"paul@inchfactory.com","duedate":"2017-09-27","status":"In Progress"}
]
}

我想要的是

{"dev@vtu.com":
[
{"id":"T-728","assignedTo":"dev","assigneemail":"dev@inchfactory.com","duedate":"2017-09-29","status":"Open"},
{"id":"T-726","assignedTo":"dev","assigneemail":"dev@inchfactory.com","duedate":"2017-10-04","status":"Open"},
{"id":"T-727","assignedTo":"dev","assigneemail":"dev@inchfactory.com","duedate":"2017-09-29","status":"In Progress"},
{"id":"T-645","assignedTo":"dev","assigneemail":"dev@inchfactory.com","duedate":"2017-09-27","status":"In Progress"}
{"id":"T-679","assignedTo":"devt","assigneemail":"dev@inchfactory.com","duedate":"2017-09-29","status":"Under Review"},
],

"paul@vtu.com":
[
{"id":"T-728","assignedTo":"paul","assigneemail":"paul@inchfactory.com","duedate":"2017-09-29","status":"Open"},
{"id":"T-726","assignedTo":"paul","assigneemail":"paul@inchfactory.com","duedate":"2017-10-04","status":"Open"},
{"id":"T-727","assignedTo":"paul","assigneemail":"paul@inchfactory.com","duedate":"2017-09-29","status":"In Progress"},
{"id":"T-645","assignedTo":"paul","assigneemail":"paul@inchfactory.com","duedate":"2017-09-27","status":"In Progress"}
{"id":"T-679","assignedTo":"paul","assigneemail":"paul@inchfactory.com","duedate":"2017-09-29","status":"Under Review"},
]
}

1 个答案:

答案 0 :(得分:0)

如果要根据id

中的数字进行排序,则可以使用此代码对对象值中的数组进行排序
function num(string) {
  return parseInt(id.replace(/[^0-9]+/g, ''));
}

function compare(a, b) {
  a = num(a.id);
  b = num(b.id);
  if (a < b) {
    return -1;
  }
  if (a > b) {
    return 1;
  }
  return 0;
}


Object.keys(obj).forEach(function(key) {
   output[key].sort(compare);
});

如果您还希望根据日期进行排序,则需要将此问题的答案合并到Sort Javascript Object Array By Date