Joe
Joe

Reputation: 4254

Using ramda to modify data in array

Input:

[
  {
    temp: "24",
    date: "2019-10-16T11:00:00.000Z"
  }
]

Output:

[[new date("2019-10-16T11:00:00.000Z").getTime(), 24]]

Got some annoying mutability problems if I do it in vanilla javascript.

Good case to use ramda.

Something like:

const convertFunc = ...
const convertArr = R.map(convertFunc)


const result = convertArr(arr);

I'm stuck. Any ideas what Ramda functions to use?

Upvotes: 1

Views: 435

Answers (2)

Ori Drori
Ori Drori

Reputation: 193037

You can map the array of objects, and use R.evolve to convert the date string to time via Date.parse(), and then get the R.props to convert to an array of arrays.

const { map, pipe, evolve, identity, props } = R

const fn = map(pipe(
  evolve({ temp: identity, date: Date.parse }),
  props(['date', 'temp'])
))

const data = [{temp: "24",date: "2019-10-16T11:00:00.000Z"}]

const result = fn(data)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>

Upvotes: 1

customcommander
customcommander

Reputation: 18961

I'm not sure Ramda would add anything substantial. Especially if you can use parameter destructuring:

map(({temp, date}) => [new Date(date).getTime(), temp],
  [{ temp: "24",
     date: "2019-10-16T11:00:00.000Z"}]);
//=> [[1571223600000, "24"]]

Upvotes: 2

Related Questions