【问题标题】:Applying a conditional filter to get unique values on JSON array应用条件过滤器获取 JSON 数组的唯一值
【发布时间】:2018-05-29 03:34:51
【问题描述】:

我有一个这样的 JSON 数组 ...

[
  {
    "Event_code": "AB-001",
    "Interest_area": "Arts and Education",
    "Start_time": "9:00 AM",
    "End_time": "3:00 PM",
    "Session_type": "Course information session"
  },
  {
    "Event_code": "AB-002",
    "Interest_area": "Arts and Education",
    "Start_time": "12:30 PM",
    "End_time": "1:00 PM",
    "Session_type": "Course information session"
  },
  {
    "Event_code": "AB-003",
    "Interest_area": "",
    "Start_time": "9:00 AM",
    "End_time": "3:00 PM",
    "Session_type": "Course information session"
  },
  {
    "Event_code": "AB-004",
    "Interest_area": "Business",
    "Start_time": "10:30 AM",
    "End_time": "11:00 AM",
    "Session_type": "Course information session"
  },
  {
    "Event_code": "AB-005",
    "Interest_area": "General Interest",
    "Start_time": "9:30 AM",
    "End_time": "1:30 PM",
    "Session_type": "Experience"
  },
  {
    "Event_code": "AB-006",
    "Interest_area": "Environment, Business",
    "Start_time": "11:00 AM",
    "End_time": "11:30 AM",
    "Session_type": "Course information session"
  },
 {
    "Event_code": "AB-014",
    "Interest_area": "Health sciences and allied health, Medicine",
    "Start_time": "1:00 PM",
    "End_time": "2:00 PM",
    "Session_type": "Course information session"
  }
    ]

我想要做的是过滤这个 JSON 并提取 unique "Interest_area" 值,其中 "Session_type" 等于 "课程说明会" ...

我的预期输出是

["艺术与教育","商业","环境"]

我已经看到this 解决方案,这与我正在寻找的非常接近,但它在我的情况下不起作用,因为我的 JSON 可能有“2 个或更多”值用于“兴趣区域”字段。

【问题讨论】:

  • 您自己尝试过什么吗?有吗?一点点?

标签: javascript jquery


【解决方案1】:

首先使用Array#filter() 过滤以获得非空兴趣和适当的会话类型

Array#reduce() 过滤结果到感兴趣的Set。一个 Set 只能有唯一值并忽略重复项

最后将Set转回数组

let interestSet = data       
  .filter(obj=> obj.Interest_area && obj.Session_type === "Course information session")   
  .reduce((a, c)=> a.add(...c.Interest_area.split(',')), new Set);
  
let uniques = [...interestSet];

console.log(uniques)
<script>
let data = [
  {
    "Event_code": "AB-001",
    "Interest_area": "Arts and Education",
    "Start_time": "9:00 AM",
    "End_time": "3:00 PM",
    "Session_type": "Course information session"
  },
  {
    "Event_code": "AB-002",
    "Interest_area": "Arts and Education",
    "Start_time": "12:30 PM",
    "End_time": "1:00 PM",
    "Session_type": "Course information session"
  },
  {
    "Event_code": "AB-003",
    "Interest_area": "",
    "Start_time": "9:00 AM",
    "End_time": "3:00 PM",
    "Session_type": "Course information session"
  },
  {
    "Event_code": "AB-004",
    "Interest_area": "Business",
    "Start_time": "10:30 AM",
    "End_time": "11:00 AM",
    "Session_type": "Course information session"
  },
  {
    "Event_code": "AB-005",
    "Interest_area": "General Interest",
    "Start_time": "9:30 AM",
    "End_time": "1:30 PM",
    "Session_type": "Experience"
  },
  {
    "Event_code": "AB-006",
    "Interest_area": "Environment,Business",
    "Start_time": "11:00 AM",
    "End_time": "11:30 AM",
    "Session_type": "Course information session"
  }
]
</script>

【讨论】:

  • 嗨@charlietfl,您的解决方案看起来不错,但它不会产生预期的输出。数组中的最后一组具有 2 个“Interest_area”键值。我需要将这些值分割成数组中的 2 个单独的值。
  • 啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊一分钟搞定
  • OK 改为设置。在那里更容易拆分倍数
  • 感谢@charlietfl 出于某种原因,当我在大型数据集上运行您的代码时,它跳过了 1 个值。
  • 对原始字符串中的特定值有什么奇怪的吗?
【解决方案2】:

你可以这样做:

let result = [...new Set(                                              //Use new Set to get unique values
   arr.filter(o=>o.Session_type === search && o.Interest_area.trim() !== '' ) //Use filter to filter the Session_type and Interest_area is not blank
      .reduce((c,v)=>c.concat(v.Interest_area.split(',')),[]))         //Use reduce and concat to flatten the array
      .map(o=>o.trim())                                                //Use map to trim the values
]

这是一个sn-p:

let arr=[{"Event_code":"AB-001","Interest_area":"Arts and Education","Start_time":"9:00 AM","End_time":"3:00 PM","Session_type":"Course information session"},{"Event_code":"AB-002","Interest_area":"Arts and Education","Start_time":"12:30 PM","End_time":"1:00 PM","Session_type":"Course information session"},{"Event_code":"AB-003","Interest_area":"","Start_time":"9:00 AM","End_time":"3:00 PM","Session_type":"Course information session"},{"Event_code":"AB-004","Interest_area":"Business","Start_time":"10:30 AM","End_time":"11:00 AM","Session_type":"Course information session"},{"Event_code":"AB-005","Interest_area":"General Interest","Start_time":"9:30 AM","End_time":"1:30 PM","Session_type":"Experience"},{"Event_code":"AB-006","Interest_area":"Environment   ,    Business       ","Start_time":"11:00 AM","End_time":"11:30 AM","Session_type":"Course information session"}];

let search = 'Course information session';
let result = [...new Set(arr.filter(o=>o.Session_type === search && o.Interest_area.trim() !== '' ).reduce((c,v)=>c.concat(v.Interest_area.split(',')),[]).map(o=>o.trim()))]

console.log(result);

文档:new Set()filter()reduce()concat()

【讨论】:

  • 谢谢@Eddie,这很好用。是否可以修剪值。数组中的一个值包含一个空格。
  • @Slyper 是的,您可以使用 map 并修剪值。我更新了我的答案。
  • Hmmmm.... 出于某种原因,当我在 Chrome 控制台中运行您的代码时,我收到此错误...“未捕获的语法错误:标识符 'arr' 已被声明”
  • 也许您已经在使用名为arr 的变量并且您正在使用let 再次启动它。您可以使用不同的变量名称。 @Slyper
  • 谢谢@Eddie。非常感谢您的所有帮助。现在一切都好。
【解决方案3】:

你必须循环每个子数组并检查会话类型的值,然后提取兴趣区域

var obj = [
  {
    "Event_code": "AB-001",
    "Interest_area": "Arts and Education",
    "Start_time": "9:00 AM",
    "End_time": "3:00 PM",
    "Session_type": "Course information session"
  },
  {
    "Event_code": "AB-002",
    "Interest_area": "Arts and Education",
    "Start_time": "12:30 PM",
    "End_time": "1:00 PM",
    "Session_type": "Course information session"
  },
  {
    "Event_code": "AB-003",
    "Interest_area": "",
    "Start_time": "9:00 AM",
    "End_time": "3:00 PM",
    "Session_type": "Course information session"
  },
  {
    "Event_code": "AB-004",
    "Interest_area": "Business",
    "Start_time": "10:30 AM",
    "End_time": "11:00 AM",
    "Session_type": "Course information session"
  },
  {
    "Event_code": "AB-005",
    "Interest_area": "General Interest",
    "Start_time": "9:30 AM",
    "End_time": "1:30 PM",
    "Session_type": "Experience"
  },
  {
    "Event_code": "AB-006",
    "Interest_area": "Environment,Business",
    "Start_time": "11:00 AM",
    "End_time": "11:30 AM",
    "Session_type": "Course information session"
  }
]

var interest_list = [];
$.each(obj, function(event,details){
  if(details['Session_type'] === 'Course information session'){
    var addnew = details['Interest_area'].split(",");
    interest_list.push(addnew);
  }
}

console.log(interest_list);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    • 1970-01-01
    • 2013-09-15
    • 2016-10-08
    • 1970-01-01
    • 2013-06-22
    • 1970-01-01
    相关资源
    最近更新 更多