使用 moment 和 angularjs 和 lodash 更改数组中的时间格式

Changing time format in an array using moment and angularjs and lodash

我有 Json 数据,其中包含基于日期的计时。

obj = {
"2017-12-08": [
    "2017-12-08T13:00:00+0530",
    "2017-12-08T15:00:00+0530",
    "2017-12-08T15:30:00+0530",
    "2017-12-08T16:00:00+0530"
],
"2017-12-09": [
    "2017-12-09T09:00:00+0530",
    "2017-12-09T09:30:00+0530",
    "2017-12-09T10:00:00+0530"
],
"2017-12-10": [
    "2017-12-10T09:00:00+0530",
    "2017-12-10T09:30:00+0530",
    "2017-12-10T10:00:00+0530",
    "2017-12-10T10:30:00+0530"
]
}

我想将此 json 对象转换为以下格式:

obj = {
"2017-12-08": ["13:00","15:00","15:30","16:00"],
"2017-12-09": ["09:00","09:30","10:00"],
"2017-12-10": ["09:00","09:30","10:00","10:30"]
}

目前我正在使用for循环遍历数组中的每个元素,然后使用

替换每个元素
obj[Object.keys(obj)[date]][time]=moment(obj[Object.keys(obj)[date]][time]).format(HH:mm);

如何使用 lodash、moment 和 angularjs 完成此操作?

为此,您只需要 momentjs,只需将日期格式化为所需的格式。 AngularJS 和 lodash 不是必需的。

var obj = {
  "2017-12-08": [
    "2017-12-08T13:00:00+0530",
    "2017-12-08T15:00:00+0530",
    "2017-12-08T15:30:00+0530",
    "2017-12-08T16:00:00+0530"
  ],
  "2017-12-09": [
    "2017-12-09T09:00:00+0530",
    "2017-12-09T09:30:00+0530",
    "2017-12-09T10:00:00+0530"
  ],
  "2017-12-10": [
    "2017-12-10T09:00:00+0530",
    "2017-12-10T09:30:00+0530",
    "2017-12-10T10:00:00+0530",
    "2017-12-10T10:30:00+0530"
  ]
};

Object.keys(obj).forEach(key => {
  obj[key] = obj[key].map(item => {
    return moment(item).format("HH:mm");
  });
});

console.log(obj);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.3/moment.min.js"></script>

这会就地修改对象。如果您不希望那样,请先克隆对象,然后再转换克隆对象。

您可以使用 _.forOwn() to loop over your object properties, _.map() 遍历每个 array 项目并使用 momentjs 更改项目格式。

你的代码应该是这样的:

_.forOwn(obj, function(value, key) {
   obj[key] = _.map(value, function(item) {
      return moment(item).format("HH:mm");
   });
});

演示:

var obj = {
  "2017-12-08": [
    "2017-12-08T13:00:00+0530",
    "2017-12-08T15:00:00+0530",
    "2017-12-08T15:30:00+0530",
    "2017-12-08T16:00:00+0530"
  ],
  "2017-12-09": [
    "2017-12-09T09:00:00+0530",
    "2017-12-09T09:30:00+0530",
    "2017-12-09T10:00:00+0530"
  ],
  "2017-12-10": [
    "2017-12-10T09:00:00+0530",
    "2017-12-10T09:30:00+0530",
    "2017-12-10T10:00:00+0530",
    "2017-12-10T10:30:00+0530"
  ]
};

_.forOwn(obj, function(value, key) {
  obj[key] = _.map(value, function(item) {
    return moment(item).format("HH:mm");
  });
});

console.log(obj);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.3/moment.min.js"></script>

或者您可以使用 _.mapValues

const obj = {
  "2017-12-08": [
    "2017-12-08T13:00:00+0530",
    "2017-12-08T15:00:00+0530",
    "2017-12-08T15:30:00+0530",
    "2017-12-08T16:00:00+0530"
  ],
  "2017-12-09": [
    "2017-12-09T09:00:00+0530",
    "2017-12-09T09:30:00+0530",
    "2017-12-09T10:00:00+0530"
  ],
  "2017-12-10": [
    "2017-12-10T09:00:00+0530",
    "2017-12-10T09:30:00+0530",
    "2017-12-10T10:00:00+0530",
    "2017-12-10T10:30:00+0530"
  ]
};

const res = _.mapValues(obj, el => el.map(d => moment(d).format('HH:mm')));

console.log(res);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.3/moment.min.js"></script>