[英]Using ramda to modify a property in array of Objects
I am trying to remove some properties from an array of objects using ramda
. 我正在尝试使用ramda
从对象数组中删除一些属性。 I have an array of properties to remove like: 我有一系列要删除的属性:
const colToHide = ['name', 'age'];
// those properties are selected by the user
And I want to remove the properties 'name'
and 'age'
(or any user selected property) from one array of objects. 我想从一组对象中删除属性'name'
和'age'
(或任何用户选择的属性)。 The array of objects is something like this: 对象数组是这样的:
const person = [
{name:'sam', age:'24', address:'xyz avenue', employed:true},
{name:'john', age:'25', address:'xyz avenue', employed:true}
];
What would be right way to update that array of object? 什么是更新对象数组的正确方法?
You can use the .omit() method in conjunction with a .map() . 您可以将.omit()方法与.map()结合使用。 Something like this: 像这样:
const person = [ {name:'sam', age:'24', address:'xyz avenue', employed:true}, {name:'john', age:'25', address:'xyz avenue', employed:true} ] const omitKeys = (keys, arr) => R.map(R.omit(keys), arr); console.log(omitKeys(["name", "age"], person));
.as-console {background-color:black !important; color:lime;} .as-console-wrapper {max-height:100% !important; top:0;}
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
Even more, you can use .compose() as @ScottSauyet proposed on the comments to do: 甚至,您可以将.compose()用作对评论进行建议的@ScottSauyet来执行:
const omitKeys = R.compose(R.map, R.omit);
And then use it as: 然后将其用作:
omitKeys(["name", "age"])(person);
const person = [ {name:'sam', age:'24', address:'xyz avenue', employed:true}, {name:'john', age:'25', address:'xyz avenue', employed:true} ] const omitKeys = R.compose(R.map, R.omit); console.log(omitKeys(["name", "age"])(person));
.as-console {background-color:black !important; color:lime;} .as-console-wrapper {max-height:100% !important; top:0;}
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
or in a point free style using R.useWith
: 或使用R.useWith
无点样式:
const omit = R.useWith(R.map, [ R.omit, R.identity, ]); const persons = [ {name:'sam', age:'24', address:'xyz avenue', employed:true}, {name:'john', age:'25', address:'xyz avenue', employed:true} ] console.log( 'result', omit(["name", "age"], persons), );
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.