使用underscorejs转换集合的更好方法

时间:2015-09-18 16:23:23

标签: javascript underscore.js

我试图用空字符串简单地替换无效日期。我在迭代一系列对象,但每当我尝试使用_.each()时,我都会迷失方向。如果有人可以告诉我一种方法来遍历列表中的所有fieldsToCheck项,那就是rad。

massage.removeBadDates = function(data){
  var fieldsToCheck = [
    "partsLeadTime",
    "statusDate",
    "targetDate",
    "revisedTargetDate",
    "quoteDate",
    "dispositionDate",
    "serviceDate",
    "finalDate",
    "receivedDate"]
  var newData = []
  _.map(data, function(value, index, list){
    newData.push(value)
    //single
    if (list[index].partsLeadTime == "1900-01-01T00:00:00"){
      newData[index].partsLeadTime = ""
    }
  });
  return newData
};

1 个答案:

答案 0 :(得分:1)

你几乎想要这样的东西:

_.each(fieldsToCheck(function(field) { 
   if (list[index][field] == "1900-01-01T00:00:00") {
    newData[index][field] = ""
  }
});