javascript - ramda 数组清理数据以生成唯一的数组

标签 javascript ramda.js

我希望使用 Ramda 获取一些数据 - 从中​​提取键值 - 并减少它,以便数组是唯一的

所以在这种情况下 - 创建一个数组 ["SONY_FINALIZING", "EXPIRE"]; - 作为额外 - 我想创建其他功能来小写值、添加连字符、驼峰式单词。

尝试使用这个但似乎无法分享 fiddle https://ramdajs.com/repl?v=0.26.1

const data2 = [
  {id: 38,
label: "ssss",
status: "SONY_FINALIZING",
region: "SIEA"},
  {id: 35,
label: "ghmjhmjhj",
status: "SONY_FINALIZING",
region: "SIEE"},
  {id: 32,
label: "gbfghfghfghg",
status: "EXPIRE",
region: "SIAE"}
]



pipe(
  groupBy(prop('id')), 
  map(pluck('status')),
  map(flatten),
  map(uniq),
)(data2)

最佳答案

使用 R.pipe 创建函数使用 R.pluck获取 status 值数组,然后 R.uniq删除重复项:

const {pipe, pluck, uniq} = R;

const fn = pipe(pluck('status'), uniq);

const data2 = [{ id: 38, label: "ssss", status: "SONY_FINALIZING", region: "SIEA" }, { id: 35, label: "ghmjhmjhj", status: "SONY_FINALIZING", region: "SIEE" }, { id: 32, label: "gbfghfghfghg", status: "EXPIRE", region: "SIAE" } ];

const result = fn(data2);

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js" integrity="sha256-buL0byPvI/XRDFscnSc/e0q+sLA65O9y+rbF+0O/4FE=" crossorigin="anonymous"></script>

关于javascript - ramda 数组清理数据以生成唯一的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60113866/

相关文章:

php - 使用 jQuery 和 PHP 发送和接收 AJAX 请求

javascript - 我可以而且应该在浏览器中实现 "desktop"应用程序吗?

javascript - 如何使函数可以使用多个参数调用,因此您可以执行 f(x,y) 而不是 f(x)(y)

javascript - lambda 斯 : locating items with an inner array that satisfies a spec

javascript - JavaScript/Ramda 中与 Clojure 的 juxt 等效的函数

JavaScript 项目从数组中消失

javascript - 使用 bigSlide jquery 插件定位菜单

javascript - 从声明它的函数外部访问变量

javascript - Ramda 函数在应用于参数时评估函数的相等性

javascript - 在 ramda.js 中使用嵌套字段进行排序