Home > Mobile >  how to transform JSON array in ramda js?
how to transform JSON array in ramda js?

Time:05-17

Input JSON

{
  'abc': [{
    'stores':[{
      'myproperty':true
    }],
    'list':[{
      'listname':'mylist',
      'listid': 'list1'
    }],
    'address': {
      'name':'george street'
    }
  }]
}

Output JSON

{
  'abc': [
    {
      myproperty: true,
      listname: 'mylist',
      listid: 'list1',
      address: 'george street'
    }
  ]
}


Need to consider only the first object in the array.

const transformArray = R.pipe(R.identity, R.applySpec({
    abc: R.pipe(R.prop('abc'), R.map(x => {
        return x.stores[0].myproperty
    }))
}));

New to Ramdajs, trying the build the functional code

CodePudding user response:

Use R.evolve transform the abc property, and then use R.applySpec with R.path to generate the internal object:

const { evolve, applySpec, path } = R

const fn = evolve({
  abc: [applySpec({
    myproperty: path(['stores', 0, 'myproperty']),
    listname: path(['list', 0, 'listname']),
    listid: path(['list', 0, 'listid']),
    address: path(['address', 'name']),
  })]
})

const obj = {
  'abc': [{
    'stores':[{
      'myproperty':true
    }],
    'list':[{
      'listname':'mylist',
      'listid': 'list1'
    }],
    'address': {
      'name':'george street'
    }
  }]
}

const result = fn(obj)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.28.0/ramda.min.js" integrity="sha512-t0vPcE8ynwIFovsylwUuLPIbdhDj6fav2prN9fEu/VYBupsmrmk9x43Hvnt Mgn2h5YPSJOk7PMo9zIeGedD1A==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

If there might be multiple items in the abc array, use R.map as well:

const { evolve, map, applySpec, path } = R

const fn = evolve({
  abc: map(applySpec({
    myproperty: path(['stores', 0, 'myproperty']),
    listname: path(['list', 0, 'listname']),
    listid: path(['list', 0, 'listid']),
    address: path(['address', 'name']),
  }))
})

const obj = {
  'abc': [{
    'stores':[{
      'myproperty':true
    }],
    'list':[{
      'listname':'mylist',
      'listid': 'list1'
    }],
    'address': {
      'name':'george street'
    }
  }]
}

const result = fn(obj)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.28.0/ramda.min.js" integrity="sha512-t0vPcE8ynwIFovsylwUuLPIbdhDj6fav2prN9fEu/VYBupsmrmk9x43Hvnt Mgn2h5YPSJOk7PMo9zIeGedD1A==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

CodePudding user response:

We can write this fairly simply using a few Ramda functions:

const convert = map (compose (mergeAll, flatten, chain (values)))

const input = {abc: [{stores: [{myproperty: true }], list: [{listname:'mylist', listid: 'list1'}], address: {name:'george street'}}]}

console .log (convert (input))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.28.0/ramda.min.js"></script>
<script> const {map, compose, mergeAll, flatten, chain, values} = R      </script>

The composed bit goes through these three steps:

// after `chain (values)`
[[{myproperty: true}], [{listname: "mylist", listid: "list1"}], {name: "george street"}]
// after `flatten`
[{myproperty: true}, {listname: "mylist", listid: "list1"}, {name: "george street"}]
// after `mergeAll`
{myproperty: true, listname: "mylist", listid: "list1", name: "george street"}

Then we apply that to all properties of our input object with map.

  • Related