如何从这个Jsonarray中删除T和Z.

时间:2021-06-16 18:53:37
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?

我想在jsonaaray之上看起来像下面的结构。不使用循环对我来说是最好的。我想要最简单的一个帮助我提高经验。你能告诉我怎么解决这个问题?

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"
}
]

3 个解决方案

#1


3  

Using the function forEach would be the cleanest way.

使用forEach函数将是最干净的方法。

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; }

#2


3  

I'm sure there is a better date related solution, but you could easily use map() to loop through and remove the letters

我确信有一个更好的日期相关的解决方案,但你可以轻松地使用map()循环并删除字母

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, '')})))

#3


1  

This will be the cleanest way:

这将是最干净的方式:

data.map(obj => {
  return { Id: obj.Id, Created_at: obj.Created_at.slice(0, -1) }
})

#1


3  

Using the function forEach would be the cleanest way.

使用forEach函数将是最干净的方法。

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; }

#2


3  

I'm sure there is a better date related solution, but you could easily use map() to loop through and remove the letters

我确信有一个更好的日期相关的解决方案,但你可以轻松地使用map()循环并删除字母

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, '')})))

#3


1  

This will be the cleanest way:

这将是最干净的方式:

data.map(obj => {
  return { Id: obj.Id, Created_at: obj.Created_at.slice(0, -1) }
})