1

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?

2 Answers 2

2

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"]]
Sign up to request clarification or add additional context in comments.

3 Comments

And there's no chance of data mutate?
There's no mutation going on in the snippet I provided.
... and of course, for the requested function, you could just write const convertArr = R.map(({temp, date}) => [new Date(date).getTime(), temp])
1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.