Lodash:替换对象数组中的值
要替换对象数组中的值,在本例中将 created_at
替换为“jan”、“Feb”等,我当然可以使用 map through ,如下所示。
但是使用 lodash 是否有更短的方法呢?
import React, { useEffect } from 'react'
import _ from 'lodash'
import moment from 'moment'
interface DataProps {
clazz_name: string
created_at: string
}
const dataone: DataProps[] = [
{
clazz_name: '1A',
created_at: '2022-01-19T09:13:42.149+08:00',
},
{
clazz_name: '1B',
created_at: '2022-02-19T09:13:42.149+08:00',
},
]
let datatwo: DataProps[] = []
function App() {
useEffect(() => {
dataone.forEach((item) =>
datatwo.push({
clazz_name: item.clazz_name,
created_at: moment(item.created_at).format('MMM'),
})
)
console.log(datatwo)
}, [])
return <div>Test</div>
}
export default App
任何提示或帮助将不胜感激。
To replace value in array of objects, and in this case the created_at
with 'jan', 'Feb' etc, I can certainly use map through as shown below.
But is there a shorter way using lodash?
import React, { useEffect } from 'react'
import _ from 'lodash'
import moment from 'moment'
interface DataProps {
clazz_name: string
created_at: string
}
const dataone: DataProps[] = [
{
clazz_name: '1A',
created_at: '2022-01-19T09:13:42.149+08:00',
},
{
clazz_name: '1B',
created_at: '2022-02-19T09:13:42.149+08:00',
},
]
let datatwo: DataProps[] = []
function App() {
useEffect(() => {
dataone.forEach((item) =>
datatwo.push({
clazz_name: item.clazz_name,
created_at: moment(item.created_at).format('MMM'),
})
)
console.log(datatwo)
}, [])
return <div>Test</div>
}
export default App
Any hint or help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
_.map
如下所示You can use
_.map
like below