Answer the question
In order to leave comments, you need to log in
How to change the value for all array elements in Ramda.js?
There is an array
let arr = [{
enabled: true,
categories: [{
enabled: false
//...
}, {
enabled: false
}, {
enabled: true
}]
}]
Answer the question
In order to leave comments, you need to log in
Not sure if this is the most concise solution..
const R = require('ramda');
const input = [{
enabled: true,
categories: [{
enabled: false,
}, {
enabled: false,
}, {
enabled: true,
}],
}];
const setEnabled = entry => {
if (entry.enabled) {
return entry;
}
return Object.assign({}, entry, { enabled: true });
};
const walkCategory = entry => {
return Object.assign({}, entry, {
categories: R.map(setEnabled, entry.categories),
});
};
const result = R.map(walkCategory, input);
console.log(JSON.stringify(result, null, 2));
[
{
"enabled": true,
"categories": [
{
"enabled": true
},
{
"enabled": true
},
{
"enabled": true
}
]
}
]
only for all elements of the categories
property
var input = [{
enabled: true,
categories: [{
enabled: false,
}, {
enabled: false,
}, {
enabled: true,
}],
}];
const walkCategory = entry => {
return Object.assign({}, entry, { enabled: true });
};
input[0].categories = R.map(walkCategory, input[0].categories);
console.log(input);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question