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

时间:2017-12-07 09:32:02

标签: arrays angularjs lodash angular-moment

我有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完成此操作?

3 个答案:

答案 0 :(得分:2)

您只需要为此设置时刻,只需格式化所需格式的日期即可。不需要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>

这会就地修改对象。如果您不想这样,请先克隆该对象,然后转换克隆。

答案 1 :(得分:1)

您可以使用 _.forOwn() 循环对象属性 _.map() 来循环遍历每个momentjs项并使用{ {1}}更改项目格式。

这应该是你的代码:

_.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>

答案 2 :(得分:1)

或者您可以使用_.mapValues

&#13;
&#13;
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);
&#13;
<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>
&#13;
&#13;
&#13;