我正在尝试从我的对象中的一个属性中删除时间。实际上我检查:索引,属性值中是否存在时间戳。其实我有这个
输入
[
{
"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/