javascript - 如何从javascript中的对象中删除时间?

标签 javascript jquery arrays datetime properties

我正在尝试从我的对象中的一个属性中删除时间。实际上我检查:索引,属性值中是否存在时间戳。其实我有这个

输入

[
  {
    "S": "Charge Interest Against Past Due Account",
    "C": "Mon Apr 13 10:38:05 GMT 2015"
  },
  {
    "S": "Charge Interest Against Past Due Account",
    "C": "Mon Apr 13 10:35:05 GMT 2015"
  }
]

输出

[
  {
    "S": "Charge Interest Against Past Due Account",
    "C": "Mon Apr 13  2015"
  },
  {
    "S": "Charge Interest Against Past Due Account",
    "C": "Mon Apr 13  2015"
  }
]

Javascript

var object = ary.map(function(o){
    var result={}
   // console.log(o)
    for(i in o){
        console.log(i+":"+o[i]) ;
        if(o[i].indexOf(":")!=-1){
            console.log("timestam present")
        }
    }
})

原始数组应该保持不变,结果应该在单独的数组中捕获。

这是 fiddle 的链接。

最佳答案

var ary=[
  {
    "S": "Charge Interest Against Past Due Account",
    "C": "Mon Apr 13 10:38:05 GMT 2015"
  },
  {
    "S": "Charge Interest Against Past Due Account",
    "C": "Mon Apr 13 10:35:05 GMT 2015"
  }]

ary.map(function(o){
        var d = new Date(o.C);
        o.C = d.toDateString();
})
console.dir(ary)

输出

C: "Mon Apr 13 2015"
S: "Charge Interest Against Past Due Account"

C: "Mon Apr 13 2015"
S: "Charge Interest Against Past Due Account"

更新

要保留原始对象并在其他对象中获取结果,请使用以下代码。

var ary=[
  {
    "S": "Charge Interest Against Past Due Account",
    "C": "Mon Apr 13 10:38:05 GMT 2015"
  },
  {
    "S": "Charge Interest Against Past Due Account",
    "C": "Mon Apr 13 10:35:05 GMT 2015"
  }]

var a = ary.map(function(o){
        var result = {};
        var d = new Date(o.C);
        result.S = o.S;
        result.C = d.toDateString();
        return result;
})
console.dir(a)

关于javascript - 如何从javascript中的对象中删除时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31368170/

相关文章:

javascript - AJAX 转 PHP 图片上传

Java,二维数组排序

javascript - 使用indexOf时数组排序不正确/不可预测?

php - WAMP 与实时服务器 facebook 连接

javascript - 如何在 Angular 或 Angularfirestore 中的 Firestore 上更新()一组 map 对象?

javascript - 我应该使用什么 JavaScript 存储库?

需要 php 数组转换帮助

javascript - 框未在 Canvas 上显示 Javascript

javascript - 如何将表输入相乘

javascript - 将图像从一个 div 动画化到另一个 div