-2

I'm trying to divide one item in array of objects by 1000 and return a new version with calculated value

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

I expect the result be like

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

any help would be appreciated.

akano1
  • 40,596
  • 19
  • 54
  • 67
  • 1
    Please post the code you've tried that isn't working, as well as the expected output. – CertainPerformance Feb 02 '19 at 18:06
  • It's really not clear what you're asking. What does it mean to divide one item in this array by 10? Maybe you want to divide a property of every item in the array by 10? – Mark Feb 02 '19 at 18:09
  • I need to divide the from element by 1000, and return the same array of object with the divided value – akano1 Feb 02 '19 at 18:12
  • Please post the actual array of objects so we can easily cut and paste it and work with it. Anyway this is a job for `map` or `forEach`, no ES6/ES7 necessary – Dexygen Feb 02 '19 at 18:12
  • `const data2 = data.map(entry => ({...entry, from: (entry.from / 1000)}))` – Per Digesen Feb 02 '19 at 18:13
  • Possible duplicate of [Update the attribute value of an object using the map function in ES6](https://stackoverflow.com/questions/49627157/update-the-attribute-value-of-an-object-using-the-map-function-in-es6) – adiga Feb 02 '19 at 20:53

3 Answers3

2

You can achieve this using map() method of arrays. If you using 0,1,2... as keys i would suggest you to use array instead. Below is example with 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)
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
0

You can use 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)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
0

You are almost there you just need to modify the from key and keep everything as it is. The destructing operator {...entry} will copy over all properties from the current element to the element which is going to be processed in the map function. Then I would only change the from key by dividing it by 1000.

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);
Fullstack Guy
  • 16,368
  • 3
  • 29
  • 44