In this lesson, we'll use Ramda's toPairs
function, along with map
, join
, concat
and compose
to create a reusable function that will convert an object to a querystring.
const R = require('ramda'); const {map, join, concat, compose, toPairs} = R; const obj = { page: 2, limit: 6, type: 'movies' }; const createQs = compose( concat('?'), // ?page=2&limit=6&type=movies join('&'), // page=2&limit=6&type=movies map(join('=')), // [ 'page=2', 'limit=6', 'type=movies' ] toPairs // [ [ 'page', 2 ], [ 'limit', 6 ], [ 'type', 'movies' ] ] ); const result = createQs(obj); console.log(result);