var data = [
{
"Id": 4,
"Created_at": "2017-11-04T15:47:17Z"
},
{
"Id": 5,
"Created_at": "2017-11-05T15:53:24Z"
},
{
"Id": 6,
"Created_at": "2017-11-05T18:59:32Z"
},
{
"Id": 7,
"Created_at": "2017-11-05T20:05:39Z"
}
]
I want above jsonaaray look like below structure. Without using loop will be best for me. I want the easiest one which help me to improve my experience. Would you please suggest me how can i solve this?
var data = [
{
"Id": 4,
"Created_at": "2017-11-04 15:47:17"
},
{
"Id": 5,
"Created_at": "2017-11-05 15:53:24"
},
{
"Id": 6,
"Created_at": "2017-11-05 18:59:32"
},
{
"Id": 7,
"Created_at": "2017-11-05 20:05:39Z"
}
]
答案 0 :(得分:3)
Using the function forEach
would be the cleanest way.
var data = [{ "Id": 4, "Created_at": "2017-11-04T15:47:17Z"},{ "Id": 5, "Created_at": "2017-11-05T15:53:24Z"},{ "Id": 6, "Created_at": "2017-11-05T18:59:32Z"},{ "Id": 7, "Created_at": "2017-11-05T20:05:39Z"}];
data.forEach(d => d.Created_at = d.Created_at.replace('T', " ").replace("Z", ""));
console.log(data)
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:3)
I'm sure there is a better date related solution, but you could easily use map() to loop through and remove the letters
data.map(item => ({ Id: item.Id, Created_at: item.Created_at.replace(/T/g,' ').replace(/Z/g, '')}))
var data = [
{
"Id": 4,
"Created_at": "2017-11-04T15:47:17Z"
},
{
"Id": 5,
"Created_at": "2017-11-05T15:53:24Z"
},
{
"Id": 6,
"Created_at": "2017-11-05T18:59:32Z"
},
{
"Id": 7,
"Created_at": "2017-11-05T20:05:39Z"
}
]
console.log(data.map(item => ({ Id: item.Id, Created_at: item.Created_at.replace(/T/g,' ').replace(/Z/g, '')})))
答案 2 :(得分:1)
这将是最干净的方式:
data.map(obj => {
return { Id: obj.Id, Created_at: obj.Created_at.slice(0, -1) }
})