Advanced Ramda Array Transformation









up vote
2
down vote

favorite












TLTR: Look at the javascript snippet.



I have an array of items.



I want to map items and change their structure.



I need to keep some properties and I also need to set a new property. However, the new property value is going to be based on the item that I am currently mapping.






// change title to whatever shape you desire
const customTitleTransformation = title => title

const arrayOfItems = [

id: 'some id 1',
isSelected: true,
title: 'some title 1',
text: 'some text',
description: 'some description'
,

id: 'some id 2',
isSelected: false,
title: 'some title 2',
text: 'some text',
description: 'some description'
,
]

// I need array of items like:
//
// id,
// isSelected,
// cells: [
// customTitleTransformation(title),
// text
// ]
//

// REGULAR JAVASCRIPT WAY
const normalizedItems = arrayOfItems.map(item => (
id: item.id,
isSelected: item.isSelected,
cells: [
customTitleTransformation(item.title),
item.text,
]
))

console.log('first result ', normalizedItems)

// USING RAMDA

const normalizedItemsRamda = R.map(
R.compose(
R.pick(['id', 'isSelected', 'cells']),
R.set(R.lensProp('cells'), ['how do I set the array?'])
)
)(arrayOfItems)

console.log('ramda result ', normalizedItemsRamda)

<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>












share|improve this question

























    up vote
    2
    down vote

    favorite












    TLTR: Look at the javascript snippet.



    I have an array of items.



    I want to map items and change their structure.



    I need to keep some properties and I also need to set a new property. However, the new property value is going to be based on the item that I am currently mapping.






    // change title to whatever shape you desire
    const customTitleTransformation = title => title

    const arrayOfItems = [

    id: 'some id 1',
    isSelected: true,
    title: 'some title 1',
    text: 'some text',
    description: 'some description'
    ,

    id: 'some id 2',
    isSelected: false,
    title: 'some title 2',
    text: 'some text',
    description: 'some description'
    ,
    ]

    // I need array of items like:
    //
    // id,
    // isSelected,
    // cells: [
    // customTitleTransformation(title),
    // text
    // ]
    //

    // REGULAR JAVASCRIPT WAY
    const normalizedItems = arrayOfItems.map(item => (
    id: item.id,
    isSelected: item.isSelected,
    cells: [
    customTitleTransformation(item.title),
    item.text,
    ]
    ))

    console.log('first result ', normalizedItems)

    // USING RAMDA

    const normalizedItemsRamda = R.map(
    R.compose(
    R.pick(['id', 'isSelected', 'cells']),
    R.set(R.lensProp('cells'), ['how do I set the array?'])
    )
    )(arrayOfItems)

    console.log('ramda result ', normalizedItemsRamda)

    <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>












    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      TLTR: Look at the javascript snippet.



      I have an array of items.



      I want to map items and change their structure.



      I need to keep some properties and I also need to set a new property. However, the new property value is going to be based on the item that I am currently mapping.






      // change title to whatever shape you desire
      const customTitleTransformation = title => title

      const arrayOfItems = [

      id: 'some id 1',
      isSelected: true,
      title: 'some title 1',
      text: 'some text',
      description: 'some description'
      ,

      id: 'some id 2',
      isSelected: false,
      title: 'some title 2',
      text: 'some text',
      description: 'some description'
      ,
      ]

      // I need array of items like:
      //
      // id,
      // isSelected,
      // cells: [
      // customTitleTransformation(title),
      // text
      // ]
      //

      // REGULAR JAVASCRIPT WAY
      const normalizedItems = arrayOfItems.map(item => (
      id: item.id,
      isSelected: item.isSelected,
      cells: [
      customTitleTransformation(item.title),
      item.text,
      ]
      ))

      console.log('first result ', normalizedItems)

      // USING RAMDA

      const normalizedItemsRamda = R.map(
      R.compose(
      R.pick(['id', 'isSelected', 'cells']),
      R.set(R.lensProp('cells'), ['how do I set the array?'])
      )
      )(arrayOfItems)

      console.log('ramda result ', normalizedItemsRamda)

      <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>












      share|improve this question













      TLTR: Look at the javascript snippet.



      I have an array of items.



      I want to map items and change their structure.



      I need to keep some properties and I also need to set a new property. However, the new property value is going to be based on the item that I am currently mapping.






      // change title to whatever shape you desire
      const customTitleTransformation = title => title

      const arrayOfItems = [

      id: 'some id 1',
      isSelected: true,
      title: 'some title 1',
      text: 'some text',
      description: 'some description'
      ,

      id: 'some id 2',
      isSelected: false,
      title: 'some title 2',
      text: 'some text',
      description: 'some description'
      ,
      ]

      // I need array of items like:
      //
      // id,
      // isSelected,
      // cells: [
      // customTitleTransformation(title),
      // text
      // ]
      //

      // REGULAR JAVASCRIPT WAY
      const normalizedItems = arrayOfItems.map(item => (
      id: item.id,
      isSelected: item.isSelected,
      cells: [
      customTitleTransformation(item.title),
      item.text,
      ]
      ))

      console.log('first result ', normalizedItems)

      // USING RAMDA

      const normalizedItemsRamda = R.map(
      R.compose(
      R.pick(['id', 'isSelected', 'cells']),
      R.set(R.lensProp('cells'), ['how do I set the array?'])
      )
      )(arrayOfItems)

      console.log('ramda result ', normalizedItemsRamda)

      <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>








      // change title to whatever shape you desire
      const customTitleTransformation = title => title

      const arrayOfItems = [

      id: 'some id 1',
      isSelected: true,
      title: 'some title 1',
      text: 'some text',
      description: 'some description'
      ,

      id: 'some id 2',
      isSelected: false,
      title: 'some title 2',
      text: 'some text',
      description: 'some description'
      ,
      ]

      // I need array of items like:
      //
      // id,
      // isSelected,
      // cells: [
      // customTitleTransformation(title),
      // text
      // ]
      //

      // REGULAR JAVASCRIPT WAY
      const normalizedItems = arrayOfItems.map(item => (
      id: item.id,
      isSelected: item.isSelected,
      cells: [
      customTitleTransformation(item.title),
      item.text,
      ]
      ))

      console.log('first result ', normalizedItems)

      // USING RAMDA

      const normalizedItemsRamda = R.map(
      R.compose(
      R.pick(['id', 'isSelected', 'cells']),
      R.set(R.lensProp('cells'), ['how do I set the array?'])
      )
      )(arrayOfItems)

      console.log('ramda result ', normalizedItemsRamda)

      <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>





      // change title to whatever shape you desire
      const customTitleTransformation = title => title

      const arrayOfItems = [

      id: 'some id 1',
      isSelected: true,
      title: 'some title 1',
      text: 'some text',
      description: 'some description'
      ,

      id: 'some id 2',
      isSelected: false,
      title: 'some title 2',
      text: 'some text',
      description: 'some description'
      ,
      ]

      // I need array of items like:
      //
      // id,
      // isSelected,
      // cells: [
      // customTitleTransformation(title),
      // text
      // ]
      //

      // REGULAR JAVASCRIPT WAY
      const normalizedItems = arrayOfItems.map(item => (
      id: item.id,
      isSelected: item.isSelected,
      cells: [
      customTitleTransformation(item.title),
      item.text,
      ]
      ))

      console.log('first result ', normalizedItems)

      // USING RAMDA

      const normalizedItemsRamda = R.map(
      R.compose(
      R.pick(['id', 'isSelected', 'cells']),
      R.set(R.lensProp('cells'), ['how do I set the array?'])
      )
      )(arrayOfItems)

      console.log('ramda result ', normalizedItemsRamda)

      <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>






      javascript node.js functional-programming ramda.js






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 21:43









      Michal

      2,2341135




      2,2341135






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          I think this may be a case for applySpec.



          The mapObject function defines the shape of the new object. That transformation is then applied to arrayOfItems.



          Whether this feels more readable is entirely left to your appreciation of course:






          // change title to whatever shape you desire
          const customTitleTransformation = title => title

          const arrayOfItems = [
          id: 'some id 1',
          isSelected: true,
          title: 'some title 1',
          text: 'some text',
          description: 'some description'
          ,

          id: 'some id 2',
          isSelected: false,
          title: 'some title 2',
          text: 'some text',
          description: 'some description'
          ,
          ]

          const mapObject = R.applySpec(
          id: R.prop('id'),
          isSelected: R.prop('isSelected'),
          cells: R.juxt([
          R.o(customTitleTransformation, R.prop('title')),
          R.prop('text')
          ])
          );

          const normalizedItemsRamda = R.map(mapObject, arrayOfItems)

          console.log('ramda result ', normalizedItemsRamda)
          console.log('nnOriginal Items Unchanged:')
          console.log(arrayOfItems);

          <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>








          share|improve this answer




















          • This is beautiful. :) I like your solution even more than pure javascript.
            – Michal
            Nov 8 at 23:57











          • Just one question where did you find R.o?
            – Michal
            Nov 9 at 0:00










          • R.o is just a simplified version of compose, taking exactly two unary functions.
            – Scott Sauyet
            Nov 9 at 0:03

















          up vote
          1
          down vote













          I'm not an expert with Ramda or FP by any stretch of the imagination, but it seems like the easiest way would be to pass a normal function into your R.compose that adds the cells property on to each object before picking the properties you want.



          Let me know if this works for you, or if this is an anti-pattern and you are strictly looking for Ramda built-ins.






          // change title to whatever shape you desire
          const customTitleTransformation = title => title

          const arrayOfItems = [

          id: 'some id 1',
          isSelected: true,
          title: 'some title 1',
          text: 'some text',
          description: 'some description'
          ,

          id: 'some id 2',
          isSelected: false,
          title: 'some title 2',
          text: 'some text',
          description: 'some description'
          ,
          ]

          // USING RAMDA

          const normalizedItemsRamda = R.map(
          R.compose(
          R.pick(['id', 'isSelected', 'cells']),
          // shallow copy, add cells property, return new updated object
          item => R.assoc('cells', [customTitleTransformation(item.title), item.text], item)
          )
          )(arrayOfItems)

          console.log('ramda result ', normalizedItemsRamda)
          console.log('nnOriginal Items Unchanged:')
          console.log(arrayOfItems);

          <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>








          share|improve this answer


















          • 1




            Nice I just noticed that you change the item.cells = ... :)
            – Michal
            Nov 8 at 22:44










          • @Michal Yeah, my first version modified the original list of items, the updated solution will leave them untouched, meaning no side-effects.
            – mhodges
            Nov 8 at 22:47










          • item => R.assoc('cells', [customTitleTransformation(item.title), item.text], item)? :)
            – Michal
            Nov 8 at 22:50










          • @Michal Yes! that looks perfect, I will update my solution
            – mhodges
            Nov 8 at 22:51






          • 1




            @Michal I can't disagree with you there, it's certainly more explicit and unambiguous to the reader.
            – mhodges
            Nov 8 at 22:57










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53216604%2fadvanced-ramda-array-transformation%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          I think this may be a case for applySpec.



          The mapObject function defines the shape of the new object. That transformation is then applied to arrayOfItems.



          Whether this feels more readable is entirely left to your appreciation of course:






          // change title to whatever shape you desire
          const customTitleTransformation = title => title

          const arrayOfItems = [
          id: 'some id 1',
          isSelected: true,
          title: 'some title 1',
          text: 'some text',
          description: 'some description'
          ,

          id: 'some id 2',
          isSelected: false,
          title: 'some title 2',
          text: 'some text',
          description: 'some description'
          ,
          ]

          const mapObject = R.applySpec(
          id: R.prop('id'),
          isSelected: R.prop('isSelected'),
          cells: R.juxt([
          R.o(customTitleTransformation, R.prop('title')),
          R.prop('text')
          ])
          );

          const normalizedItemsRamda = R.map(mapObject, arrayOfItems)

          console.log('ramda result ', normalizedItemsRamda)
          console.log('nnOriginal Items Unchanged:')
          console.log(arrayOfItems);

          <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>








          share|improve this answer




















          • This is beautiful. :) I like your solution even more than pure javascript.
            – Michal
            Nov 8 at 23:57











          • Just one question where did you find R.o?
            – Michal
            Nov 9 at 0:00










          • R.o is just a simplified version of compose, taking exactly two unary functions.
            – Scott Sauyet
            Nov 9 at 0:03














          up vote
          2
          down vote



          accepted










          I think this may be a case for applySpec.



          The mapObject function defines the shape of the new object. That transformation is then applied to arrayOfItems.



          Whether this feels more readable is entirely left to your appreciation of course:






          // change title to whatever shape you desire
          const customTitleTransformation = title => title

          const arrayOfItems = [
          id: 'some id 1',
          isSelected: true,
          title: 'some title 1',
          text: 'some text',
          description: 'some description'
          ,

          id: 'some id 2',
          isSelected: false,
          title: 'some title 2',
          text: 'some text',
          description: 'some description'
          ,
          ]

          const mapObject = R.applySpec(
          id: R.prop('id'),
          isSelected: R.prop('isSelected'),
          cells: R.juxt([
          R.o(customTitleTransformation, R.prop('title')),
          R.prop('text')
          ])
          );

          const normalizedItemsRamda = R.map(mapObject, arrayOfItems)

          console.log('ramda result ', normalizedItemsRamda)
          console.log('nnOriginal Items Unchanged:')
          console.log(arrayOfItems);

          <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>








          share|improve this answer




















          • This is beautiful. :) I like your solution even more than pure javascript.
            – Michal
            Nov 8 at 23:57











          • Just one question where did you find R.o?
            – Michal
            Nov 9 at 0:00










          • R.o is just a simplified version of compose, taking exactly two unary functions.
            – Scott Sauyet
            Nov 9 at 0:03












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          I think this may be a case for applySpec.



          The mapObject function defines the shape of the new object. That transformation is then applied to arrayOfItems.



          Whether this feels more readable is entirely left to your appreciation of course:






          // change title to whatever shape you desire
          const customTitleTransformation = title => title

          const arrayOfItems = [
          id: 'some id 1',
          isSelected: true,
          title: 'some title 1',
          text: 'some text',
          description: 'some description'
          ,

          id: 'some id 2',
          isSelected: false,
          title: 'some title 2',
          text: 'some text',
          description: 'some description'
          ,
          ]

          const mapObject = R.applySpec(
          id: R.prop('id'),
          isSelected: R.prop('isSelected'),
          cells: R.juxt([
          R.o(customTitleTransformation, R.prop('title')),
          R.prop('text')
          ])
          );

          const normalizedItemsRamda = R.map(mapObject, arrayOfItems)

          console.log('ramda result ', normalizedItemsRamda)
          console.log('nnOriginal Items Unchanged:')
          console.log(arrayOfItems);

          <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>








          share|improve this answer












          I think this may be a case for applySpec.



          The mapObject function defines the shape of the new object. That transformation is then applied to arrayOfItems.



          Whether this feels more readable is entirely left to your appreciation of course:






          // change title to whatever shape you desire
          const customTitleTransformation = title => title

          const arrayOfItems = [
          id: 'some id 1',
          isSelected: true,
          title: 'some title 1',
          text: 'some text',
          description: 'some description'
          ,

          id: 'some id 2',
          isSelected: false,
          title: 'some title 2',
          text: 'some text',
          description: 'some description'
          ,
          ]

          const mapObject = R.applySpec(
          id: R.prop('id'),
          isSelected: R.prop('isSelected'),
          cells: R.juxt([
          R.o(customTitleTransformation, R.prop('title')),
          R.prop('text')
          ])
          );

          const normalizedItemsRamda = R.map(mapObject, arrayOfItems)

          console.log('ramda result ', normalizedItemsRamda)
          console.log('nnOriginal Items Unchanged:')
          console.log(arrayOfItems);

          <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>








          // change title to whatever shape you desire
          const customTitleTransformation = title => title

          const arrayOfItems = [
          id: 'some id 1',
          isSelected: true,
          title: 'some title 1',
          text: 'some text',
          description: 'some description'
          ,

          id: 'some id 2',
          isSelected: false,
          title: 'some title 2',
          text: 'some text',
          description: 'some description'
          ,
          ]

          const mapObject = R.applySpec(
          id: R.prop('id'),
          isSelected: R.prop('isSelected'),
          cells: R.juxt([
          R.o(customTitleTransformation, R.prop('title')),
          R.prop('text')
          ])
          );

          const normalizedItemsRamda = R.map(mapObject, arrayOfItems)

          console.log('ramda result ', normalizedItemsRamda)
          console.log('nnOriginal Items Unchanged:')
          console.log(arrayOfItems);

          <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>





          // change title to whatever shape you desire
          const customTitleTransformation = title => title

          const arrayOfItems = [
          id: 'some id 1',
          isSelected: true,
          title: 'some title 1',
          text: 'some text',
          description: 'some description'
          ,

          id: 'some id 2',
          isSelected: false,
          title: 'some title 2',
          text: 'some text',
          description: 'some description'
          ,
          ]

          const mapObject = R.applySpec(
          id: R.prop('id'),
          isSelected: R.prop('isSelected'),
          cells: R.juxt([
          R.o(customTitleTransformation, R.prop('title')),
          R.prop('text')
          ])
          );

          const normalizedItemsRamda = R.map(mapObject, arrayOfItems)

          console.log('ramda result ', normalizedItemsRamda)
          console.log('nnOriginal Items Unchanged:')
          console.log(arrayOfItems);

          <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 23:51









          customcommander

          595213




          595213











          • This is beautiful. :) I like your solution even more than pure javascript.
            – Michal
            Nov 8 at 23:57











          • Just one question where did you find R.o?
            – Michal
            Nov 9 at 0:00










          • R.o is just a simplified version of compose, taking exactly two unary functions.
            – Scott Sauyet
            Nov 9 at 0:03
















          • This is beautiful. :) I like your solution even more than pure javascript.
            – Michal
            Nov 8 at 23:57











          • Just one question where did you find R.o?
            – Michal
            Nov 9 at 0:00










          • R.o is just a simplified version of compose, taking exactly two unary functions.
            – Scott Sauyet
            Nov 9 at 0:03















          This is beautiful. :) I like your solution even more than pure javascript.
          – Michal
          Nov 8 at 23:57





          This is beautiful. :) I like your solution even more than pure javascript.
          – Michal
          Nov 8 at 23:57













          Just one question where did you find R.o?
          – Michal
          Nov 9 at 0:00




          Just one question where did you find R.o?
          – Michal
          Nov 9 at 0:00












          R.o is just a simplified version of compose, taking exactly two unary functions.
          – Scott Sauyet
          Nov 9 at 0:03




          R.o is just a simplified version of compose, taking exactly two unary functions.
          – Scott Sauyet
          Nov 9 at 0:03












          up vote
          1
          down vote













          I'm not an expert with Ramda or FP by any stretch of the imagination, but it seems like the easiest way would be to pass a normal function into your R.compose that adds the cells property on to each object before picking the properties you want.



          Let me know if this works for you, or if this is an anti-pattern and you are strictly looking for Ramda built-ins.






          // change title to whatever shape you desire
          const customTitleTransformation = title => title

          const arrayOfItems = [

          id: 'some id 1',
          isSelected: true,
          title: 'some title 1',
          text: 'some text',
          description: 'some description'
          ,

          id: 'some id 2',
          isSelected: false,
          title: 'some title 2',
          text: 'some text',
          description: 'some description'
          ,
          ]

          // USING RAMDA

          const normalizedItemsRamda = R.map(
          R.compose(
          R.pick(['id', 'isSelected', 'cells']),
          // shallow copy, add cells property, return new updated object
          item => R.assoc('cells', [customTitleTransformation(item.title), item.text], item)
          )
          )(arrayOfItems)

          console.log('ramda result ', normalizedItemsRamda)
          console.log('nnOriginal Items Unchanged:')
          console.log(arrayOfItems);

          <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>








          share|improve this answer


















          • 1




            Nice I just noticed that you change the item.cells = ... :)
            – Michal
            Nov 8 at 22:44










          • @Michal Yeah, my first version modified the original list of items, the updated solution will leave them untouched, meaning no side-effects.
            – mhodges
            Nov 8 at 22:47










          • item => R.assoc('cells', [customTitleTransformation(item.title), item.text], item)? :)
            – Michal
            Nov 8 at 22:50










          • @Michal Yes! that looks perfect, I will update my solution
            – mhodges
            Nov 8 at 22:51






          • 1




            @Michal I can't disagree with you there, it's certainly more explicit and unambiguous to the reader.
            – mhodges
            Nov 8 at 22:57














          up vote
          1
          down vote













          I'm not an expert with Ramda or FP by any stretch of the imagination, but it seems like the easiest way would be to pass a normal function into your R.compose that adds the cells property on to each object before picking the properties you want.



          Let me know if this works for you, or if this is an anti-pattern and you are strictly looking for Ramda built-ins.






          // change title to whatever shape you desire
          const customTitleTransformation = title => title

          const arrayOfItems = [

          id: 'some id 1',
          isSelected: true,
          title: 'some title 1',
          text: 'some text',
          description: 'some description'
          ,

          id: 'some id 2',
          isSelected: false,
          title: 'some title 2',
          text: 'some text',
          description: 'some description'
          ,
          ]

          // USING RAMDA

          const normalizedItemsRamda = R.map(
          R.compose(
          R.pick(['id', 'isSelected', 'cells']),
          // shallow copy, add cells property, return new updated object
          item => R.assoc('cells', [customTitleTransformation(item.title), item.text], item)
          )
          )(arrayOfItems)

          console.log('ramda result ', normalizedItemsRamda)
          console.log('nnOriginal Items Unchanged:')
          console.log(arrayOfItems);

          <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>








          share|improve this answer


















          • 1




            Nice I just noticed that you change the item.cells = ... :)
            – Michal
            Nov 8 at 22:44










          • @Michal Yeah, my first version modified the original list of items, the updated solution will leave them untouched, meaning no side-effects.
            – mhodges
            Nov 8 at 22:47










          • item => R.assoc('cells', [customTitleTransformation(item.title), item.text], item)? :)
            – Michal
            Nov 8 at 22:50










          • @Michal Yes! that looks perfect, I will update my solution
            – mhodges
            Nov 8 at 22:51






          • 1




            @Michal I can't disagree with you there, it's certainly more explicit and unambiguous to the reader.
            – mhodges
            Nov 8 at 22:57












          up vote
          1
          down vote










          up vote
          1
          down vote









          I'm not an expert with Ramda or FP by any stretch of the imagination, but it seems like the easiest way would be to pass a normal function into your R.compose that adds the cells property on to each object before picking the properties you want.



          Let me know if this works for you, or if this is an anti-pattern and you are strictly looking for Ramda built-ins.






          // change title to whatever shape you desire
          const customTitleTransformation = title => title

          const arrayOfItems = [

          id: 'some id 1',
          isSelected: true,
          title: 'some title 1',
          text: 'some text',
          description: 'some description'
          ,

          id: 'some id 2',
          isSelected: false,
          title: 'some title 2',
          text: 'some text',
          description: 'some description'
          ,
          ]

          // USING RAMDA

          const normalizedItemsRamda = R.map(
          R.compose(
          R.pick(['id', 'isSelected', 'cells']),
          // shallow copy, add cells property, return new updated object
          item => R.assoc('cells', [customTitleTransformation(item.title), item.text], item)
          )
          )(arrayOfItems)

          console.log('ramda result ', normalizedItemsRamda)
          console.log('nnOriginal Items Unchanged:')
          console.log(arrayOfItems);

          <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>








          share|improve this answer














          I'm not an expert with Ramda or FP by any stretch of the imagination, but it seems like the easiest way would be to pass a normal function into your R.compose that adds the cells property on to each object before picking the properties you want.



          Let me know if this works for you, or if this is an anti-pattern and you are strictly looking for Ramda built-ins.






          // change title to whatever shape you desire
          const customTitleTransformation = title => title

          const arrayOfItems = [

          id: 'some id 1',
          isSelected: true,
          title: 'some title 1',
          text: 'some text',
          description: 'some description'
          ,

          id: 'some id 2',
          isSelected: false,
          title: 'some title 2',
          text: 'some text',
          description: 'some description'
          ,
          ]

          // USING RAMDA

          const normalizedItemsRamda = R.map(
          R.compose(
          R.pick(['id', 'isSelected', 'cells']),
          // shallow copy, add cells property, return new updated object
          item => R.assoc('cells', [customTitleTransformation(item.title), item.text], item)
          )
          )(arrayOfItems)

          console.log('ramda result ', normalizedItemsRamda)
          console.log('nnOriginal Items Unchanged:')
          console.log(arrayOfItems);

          <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>








          // change title to whatever shape you desire
          const customTitleTransformation = title => title

          const arrayOfItems = [

          id: 'some id 1',
          isSelected: true,
          title: 'some title 1',
          text: 'some text',
          description: 'some description'
          ,

          id: 'some id 2',
          isSelected: false,
          title: 'some title 2',
          text: 'some text',
          description: 'some description'
          ,
          ]

          // USING RAMDA

          const normalizedItemsRamda = R.map(
          R.compose(
          R.pick(['id', 'isSelected', 'cells']),
          // shallow copy, add cells property, return new updated object
          item => R.assoc('cells', [customTitleTransformation(item.title), item.text], item)
          )
          )(arrayOfItems)

          console.log('ramda result ', normalizedItemsRamda)
          console.log('nnOriginal Items Unchanged:')
          console.log(arrayOfItems);

          <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>





          // change title to whatever shape you desire
          const customTitleTransformation = title => title

          const arrayOfItems = [

          id: 'some id 1',
          isSelected: true,
          title: 'some title 1',
          text: 'some text',
          description: 'some description'
          ,

          id: 'some id 2',
          isSelected: false,
          title: 'some title 2',
          text: 'some text',
          description: 'some description'
          ,
          ]

          // USING RAMDA

          const normalizedItemsRamda = R.map(
          R.compose(
          R.pick(['id', 'isSelected', 'cells']),
          // shallow copy, add cells property, return new updated object
          item => R.assoc('cells', [customTitleTransformation(item.title), item.text], item)
          )
          )(arrayOfItems)

          console.log('ramda result ', normalizedItemsRamda)
          console.log('nnOriginal Items Unchanged:')
          console.log(arrayOfItems);

          <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 8 at 22:52

























          answered Nov 8 at 22:29









          mhodges

          7,38411131




          7,38411131







          • 1




            Nice I just noticed that you change the item.cells = ... :)
            – Michal
            Nov 8 at 22:44










          • @Michal Yeah, my first version modified the original list of items, the updated solution will leave them untouched, meaning no side-effects.
            – mhodges
            Nov 8 at 22:47










          • item => R.assoc('cells', [customTitleTransformation(item.title), item.text], item)? :)
            – Michal
            Nov 8 at 22:50










          • @Michal Yes! that looks perfect, I will update my solution
            – mhodges
            Nov 8 at 22:51






          • 1




            @Michal I can't disagree with you there, it's certainly more explicit and unambiguous to the reader.
            – mhodges
            Nov 8 at 22:57












          • 1




            Nice I just noticed that you change the item.cells = ... :)
            – Michal
            Nov 8 at 22:44










          • @Michal Yeah, my first version modified the original list of items, the updated solution will leave them untouched, meaning no side-effects.
            – mhodges
            Nov 8 at 22:47










          • item => R.assoc('cells', [customTitleTransformation(item.title), item.text], item)? :)
            – Michal
            Nov 8 at 22:50










          • @Michal Yes! that looks perfect, I will update my solution
            – mhodges
            Nov 8 at 22:51






          • 1




            @Michal I can't disagree with you there, it's certainly more explicit and unambiguous to the reader.
            – mhodges
            Nov 8 at 22:57







          1




          1




          Nice I just noticed that you change the item.cells = ... :)
          – Michal
          Nov 8 at 22:44




          Nice I just noticed that you change the item.cells = ... :)
          – Michal
          Nov 8 at 22:44












          @Michal Yeah, my first version modified the original list of items, the updated solution will leave them untouched, meaning no side-effects.
          – mhodges
          Nov 8 at 22:47




          @Michal Yeah, my first version modified the original list of items, the updated solution will leave them untouched, meaning no side-effects.
          – mhodges
          Nov 8 at 22:47












          item => R.assoc('cells', [customTitleTransformation(item.title), item.text], item)? :)
          – Michal
          Nov 8 at 22:50




          item => R.assoc('cells', [customTitleTransformation(item.title), item.text], item)? :)
          – Michal
          Nov 8 at 22:50












          @Michal Yes! that looks perfect, I will update my solution
          – mhodges
          Nov 8 at 22:51




          @Michal Yes! that looks perfect, I will update my solution
          – mhodges
          Nov 8 at 22:51




          1




          1




          @Michal I can't disagree with you there, it's certainly more explicit and unambiguous to the reader.
          – mhodges
          Nov 8 at 22:57




          @Michal I can't disagree with you there, it's certainly more explicit and unambiguous to the reader.
          – mhodges
          Nov 8 at 22:57

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53216604%2fadvanced-ramda-array-transformation%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

          Edmonton

          Crossroads (UK TV series)