ES6:如何遍历对象数组并更改其中的一项

ES6: How to go through an array of object and change one of the items in there

我正在尝试将对象数组中的一项除以 1000 和 return 一个具有计算值的新版本

0: {name: "Mon, 28", from: 10236, to: -0, time: "2019-01-28T18:51:04+01:00"}
1: {name: "Tue, 29", from: 10209, to: -0, time: "2019-01-29T18:51:03+01:00"}
2: {name: "Wed, 30", from: 12088, to: -0, time: "2019-01-30T18:51:01+01:00"}
3: {name: "Thu, 31", from: 10789, to: -0, time: "2019-01-31T18:50:59+01:00"}
4: {name: "Fri, 1", from: 11449, to: -0, time: "2019-02-01T18:50:56+01:00"}
5: {name: "Sat, 2", from: 13404, to: -0, time: "2019-02-02T18:50:48+01:00"}

const data2 = data.map(entry => {
        let rObj = {}
        rObj[entry.key] = entry.name
        rObj[entry.from] = entry.from / 1000
        rObj[entry.to] = entry.to
        rObj[entry.time] = entry.time
        return rObj
        // return entry.from
    })

我希望结果像

0: {name: "Mon, 28", from: 10.236, to: -0, time: "2019-01-28T18:51:04+01:00"}
1: {name: "Tue, 29", from: 10.209, to: -0, time: "2019-01-29T18:51:03+01:00"}
2: {name: "Wed, 30", from: 12.088, to: -0, time: "2019-01-30T18:51:01+01:00"}
3: {name: "Thu, 31", from: 10.789, to: -0, time: "2019-01-31T18:50:59+01:00"}
4: {name: "Fri, 1", from: 11.449, to: -0, time: "2019-02-01T18:50:56+01:00"}
5: {name: "Sat, 2", from: 13.404, to: -0, time: "2019-02-02T18:50:48+01:00"}

如有任何帮助,我们将不胜感激。

您可以使用 arraysmap() 方法实现此目的。如果您将 0,1,2... 用作 keys,我建议您改用 array。下面是数组的例子

const arr = [{name: "Mon, 28", from: 10236, to: -0, time: "2019-01-28T18:51:04+01:00"},
{name: "Tue, 29", from: 10.209, to: -0, time: "2019-01-29T18:51:03+01:00"},
{name: "Wed, 30", from: 12.088, to: -0, time: "2019-01-30T18:51:01+01:00"},
{name: "Thu, 31", from: 10.789, to: -0, time: "2019-01-31T18:50:59+01:00"},
{name: "Fri, 1", from: 11.449, to: -0, time: "2019-02-01T18:50:56+01:00"},
{name: "Sat, 2", from: 13.404, to: -0, time: "2019-02-02T18:50:48+01:00"}]

const newArr = arr.map(item => ({...item,from:item.from/1000}))
console.log(newArr)

您可以使用map and destructing assignment

let obj = [{name: "Mon, 28", from: 10236, to: -0, time: "2019-01-28T18:51:04+01:00"},{name: "Tue, 29", from: 10209, to: -0, time: "2019-01-29T18:51:03+01:00"},{name: "Wed, 30", from: 12088, to: -0, time: "2019-01-30T18:51:01+01:00"},{name: "Thu, 31", from: 10789, to: -0, time: "2019-01-31T18:50:59+01:00"},{name: "Fri, 1", from: 11449, to: -0, time: "2019-02-01T18:50:56+01:00"},{name: "Sat, 2", from: 13404, to: -0, time: "2019-02-02T18:50:48+01:00"}]

const op = obj.map(e=>({...e, from: e.from/1000}))

console.log(op)

你几乎完成了,你只需要修改 from 键并保持一切不变。析构运算符 {...entry} 会将当前元素的所有属性复制到将在 map 函数中处理的元素。然后我只会通过将其除以 1000 来更改 from 键。

const data =  [{name: "Mon, 28", from: 10236, to: -0, time: "2019-01-28T18:51:04+01:00"},{name: "Tue, 29", from: 10209, to: -0, time: "2019-01-29T18:51:03+01:00"},{name: "Wed, 30", from: 12088, to: -0, time: "2019-01-30T18:51:01+01:00"},{name: "Thu, 31", from: 10789, to: -0, time: "2019-01-31T18:50:59+01:00"},{name: "Fri, 1", from: 11449, to: -0, time: "2019-02-01T18:50:56+01:00"},{name: "Sat, 2", from: 13404, to: -0, time: "2019-02-02T18:50:48+01:00"}]
const data2 = data.map(entry => {
        const rObj = {...entry}
        rObj['from'] = entry['from']/1000;
        return rObj;          
    })
console.log(data2);