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>
javascript node.js functional-programming ramda.js
add a comment |
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>
javascript node.js functional-programming ramda.js
add a comment |
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>
javascript node.js functional-programming ramda.js
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
javascript node.js functional-programming ramda.js
asked Nov 8 at 21:43
Michal
2,2341135
2,2341135
add a comment |
add a comment |
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>
This is beautiful. :) I like your solution even more than pure javascript.
– Michal
Nov 8 at 23:57
Just one question where did you findR.o
?
– Michal
Nov 9 at 0:00
R.o
is just a simplified version ofcompose
, taking exactly two unary functions.
– Scott Sauyet
Nov 9 at 0:03
add a comment |
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 pick
ing 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>
1
Nice I just noticed that you change theitem.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
|
show 1 more comment
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>
This is beautiful. :) I like your solution even more than pure javascript.
– Michal
Nov 8 at 23:57
Just one question where did you findR.o
?
– Michal
Nov 9 at 0:00
R.o
is just a simplified version ofcompose
, taking exactly two unary functions.
– Scott Sauyet
Nov 9 at 0:03
add a comment |
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>
This is beautiful. :) I like your solution even more than pure javascript.
– Michal
Nov 8 at 23:57
Just one question where did you findR.o
?
– Michal
Nov 9 at 0:00
R.o
is just a simplified version ofcompose
, taking exactly two unary functions.
– Scott Sauyet
Nov 9 at 0:03
add a comment |
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>
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>
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 findR.o
?
– Michal
Nov 9 at 0:00
R.o
is just a simplified version ofcompose
, taking exactly two unary functions.
– Scott Sauyet
Nov 9 at 0:03
add a comment |
This is beautiful. :) I like your solution even more than pure javascript.
– Michal
Nov 8 at 23:57
Just one question where did you findR.o
?
– Michal
Nov 9 at 0:00
R.o
is just a simplified version ofcompose
, 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
add a comment |
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 pick
ing 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>
1
Nice I just noticed that you change theitem.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
|
show 1 more comment
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 pick
ing 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>
1
Nice I just noticed that you change theitem.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
|
show 1 more comment
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 pick
ing 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>
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 pick
ing 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>
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 theitem.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
|
show 1 more comment
1
Nice I just noticed that you change theitem.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
|
show 1 more comment
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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