管道,带有动态参数的 ramda

cons columnDefs = [

   {

     label: 'The_First_Name',

     value: getProp,

     args: ['firstName'] // different number of arguments depending on function

   },

   {

     label: 'City',

     value: getNestedProperty,

     args: ['location', 'city'] 

   }

]


const data = [

  {

     firstName: 'Joe',

     lastName: 'Smith',

     location: {

       city: 'London'

     }

  },

   {

     firstName: 'Anna',

     lastName: 'Andersson',

     location: {

       city: 'Stockholm'

     }

  }

]


const getProp = (object, key) => R.prop(key, object);


const getNestedProperty = (obj, args) => R.path(..args, obj);

用于映射数据的 Ramda 管道:


const tableBuilder = R.pipe(R.map); // some ramda functions in here


const rows = tableBuilder(data, columnDefs);

想要的输出:


rows output:


[

   {

      The_First_Name: 'Joe',

      city: 'London'

   },

   {

      The_First_Name: 'Anna',

      city: 'Stockholm'

   }

]

每一行的关键是label在属性columnDefs。该值Ramda与valueprop 中定义的参数一起从prop 中的函数中获取args。


https://plnkr.co/edit/rOGh4zkyOEF24TLaCZ4e?p=preview


完全卡住了。这甚至可能与 Ramda 有关吗?还是用普通的 javascript 来做更好?


守着星空守着你
浏览 135回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript