如何使用join-then-sum插件合并并求和JavaScript数组对象

需积分: 21 0 下载量 81 浏览量 更新于2024-12-10 收藏 4KB ZIP 举报
资源摘要信息:"join-then-sum:加入一个键上的对象数组,然后对连接中的元素求和" 在编程世界中,处理数据集是日常任务的一部分。JavaScript是一种强大的编程语言,广泛用于网页和服务器端的编程。它为开发者提供了一系列工具来管理和操作数据结构。标题“join-then-sum: 加入一个键上的对象数组,然后对连接中的元素求和”中描述了一种特定的数据操作流程,它是针对对象数组进行的一种分组求和操作。 首先,我们来解释标题中提到的几个关键概念。 “join”一词在数据处理上下文中通常指的是数据聚合。在这个特定的上下文中,它指的是一组对象根据特定的键值连接在一起。在给定的例子中,我们有以下对象数组: ```javascript data = [ { 'i': 0, 'j': 0, 'v': 1 }, { 'i': 0, 'j': 0, 'v': 2 }, { 'i': 1, 'j': 0, 'v': 3 }, { 'i': 1, 'j': 1, 'v': 4 }, { 'i': 1, 'j': 1, 'v': '5' } ] ``` 数组中的每个对象包含了三个属性:`i`、`j`和`v`。其中`i`和`j`将被用于分组,而`v`是需要进行求和的值。 “sum”则是指对一组数值进行相加的数学运算。在此场景中,我们将对分组后的对象数组中的`v`属性值进行求和。 描述中提到的`var joinsum = require('join-then-sum')`显示了如何引入一个名为`join-then-sum`的npm模块,这个模块可能是专门为完成此类操作而设计的。在Node.js环境中,`require`是加载模块的标准方式。如果这个模块不存在,那可能是一个假设的库或需要用户自己实现。 在实际应用中,可以想象我们有大量记录,每条记录可能包含一些标识符(例如`i`和`j`),以及一些数值(例如`v`)。我们想要基于这些标识符对数值进行分组,并计算每个组的总和。这在统计、数据分析、财务报告等领域非常有用。 例子中展示了一种可能的JavaScript实现方式,其中我们使用了`joinsum`函数。这个函数的参数包括一个对象数组和一个键数组。键数组指明了我们基于哪些键值进行分组(`['i', 'j']`),而对象数组是我们需要处理的数据。 在执行`joinsum(data, ['i', 'j'])`后,我们期望得到一个按照`i`和`j`属性值分组的数组,每个分组中包含`v`属性值的总和。 在JavaScript中实现这种功能,一般会涉及以下步骤: 1. 遍历对象数组。 2. 使用键数组中的值作为新对象的属性,创建一个新的对象用于分组和存储求和结果。 3. 检查新对象中是否已存在当前遍历到的对象的键对应的属性,如果不存在,则初始化为0。 4. 将当前对象的`v`值加到新对象中相应属性的值上。 5. 经过遍历后,新对象中就存储了每个分组的`v`值总和。 最终,我们将会得到一个每个键对应的分组求和结果,可以用于进一步的数据分析或展示。 使用“join-then-sum”这样的工具或函数库,可以简化开发过程,提高代码的可读性和可维护性。这在处理大量数据时尤其有用,因为可以避免编写复杂的循环和条件语句,从而减少错误的发生并提升性能。 总之,“join-then-sum”提供了一种高效的方式来对对象数组进行分组和求和操作,这对于数据处理是一个非常实用的技术。在实际的项目开发中,我们可能会经常遇到需要对数据进行分组求和的场景,熟练掌握这类操作能够帮助我们更加高效地编写代码。

select a.IMSI, a.GJ, a.YYS, count(case when substr(b.IMSI,1,5)<>46000 and b.operate_code=2 then a.IMSI else null end) as MRWZGXQQCS, count(case when substr(b.IMSI,1,5)<>46000 and b.operate_code=2 and b.result<>1 then a.IMSI else null end) as MRWZGXCGCS, case when count(case when substr(b.IMSI,1,5)<>46000 and b.operate_code=2 then a.IMSI else null end)=0 then 0 else count(case when substr(b.IMSI,1,5)<>46000 and b.operate_code=2 and b.result<>1 then a.IMSI else null)/count(case when substr(b.IMSI,1,5)<>46000 and b.operate_code=2 then a.IMSI else null end) as MRWZGXCGL, sum(c.MRZJCS) as MRZJCS, sum(c.MRZJHJCGCS) as MRZJHJCGCS, sum(d.MRBJCS) as MRBJCS, sum(d.MRBJHJCGCS) as MRBJHJCGCS, case when sum(c.MRZJCS)=0 then 0 else sum(c.MRZJHJCGCS)/sum(c.MRZJCS) end as MRZJWLJTL, case when sum(c.MRZJCS)=0 then 0 else sum(d.MRBJHJCGCS)/sum(d.MRBJCS) end as MRBJWLJTL, count(case when substr(b.msc,1,2)<>86 and a.GJ is not null and b.operate_code=2 then a.IMSI else null end) as MCWZGXQQCS, count(case when substr(b.msc,1,2)<>86 and a.GJ is not null and b.operate_code=2 and b.result<>1 then a.IMSI else null end) as MCWZGXCGCS, case when count(case when substr(b.msc,1,2)<>86 and a.GJ is not null and b.operate_code=2 then a.IMSI else null end)=0 then 0 else count(case when substr(b.msc,1,2)<>86 and a.GJ is not null and b.operate_code=2 and b.result<>1 then a.IMSI else null end)/ count(case when substr(b.msc,1,2)<>86 and a.GJ is not null and b.operate_code=2 then a.IMSI else null end) as MCWZGXCGL from (select * from TAB_A union select * from TAB_B union select * from TAB_C)a left join (select * from spark_odc_dwd.D_ENS_GMAP_MM where p_hour='#{time yyyyMMddHH}') b on a.IMSI=b.IMSI left join TAB_D c on a.IMSI=c.calling_imsi left join TAB_E d on a.IMSI=d.called_imsi group by a.IMSI, a.GJ, a.YYS

2023-06-07 上传

select '#{time yyyyMMdd}0000' TIME_ID, a.IMSI, a.GJ, a.YYS, count(case when substr(b.IMSI,1,5)<>46000 and b.operate_code=2 then a.IMSI else null end) as MRWZGXQQCS, count(case when substr(b.IMSI,1,5)<>46000 and b.operate_code=2 and b.result<>1 then a.IMSI else null end) as MRWZGXCGCS, case when count(case when substr(b.IMSI,1,5)<>46000 and b.operate_code=2 then a.IMSI else null end)=0 then 0 else count(case when substr(b.IMSI,1,5)<>46000 and b.operate_code=2 and b.result<>1 then a.IMSI else null)/count(case when substr(b.IMSI,1,5)<>46000 and b.operate_code=2 then a.IMSI else null end) as MRWZGXCGL, sum(c.MRZJCS) as MRZJCS, sum(c.MRZJHJCGCS) as MRZJHJCGCS, sum(d.MRBJCS) as MRBJCS, sum(d.MRBJHJCGCS) as MRBJHJCGCS, case when sum(c.MRZJCS)=0 then 0 else sum(c.MRZJHJCGCS)/sum(c.MRZJCS) end as MRZJWLJTL, case when sum(c.MRZJCS)=0 then 0 else sum(d.MRBJHJCGCS)/sum(d.MRBJCS) end as MRBJWLJTL, count(case when substr(b.msc,1,2)<>86 and a.GJ is not null and b.operate_code=2 then a.IMSI else null end) as MCWZGXQQCS, count(case when substr(b.msc,1,2)<>86 and a.GJ is not null and b.operate_code=2 and b.result<>1 then a.IMSI else null end) as MCWZGXCGCS, case when count(case when substr(b.msc,1,2)<>86 and a.GJ is not null and b.operate_code=2 then a.IMSI else null end)=0 then 0 else count(case when substr(b.msc,1,2)<>86 and a.GJ is not null and b.operate_code=2 and b.result<>1 then a.IMSI else null end)/ count(case when substr(b.msc,1,2)<>86 and a.GJ is not null and b.operate_code=2 then a.IMSI else null end) as MCWZGXCGL, now() LOAD_TIME from (select * from TAB_A union select * from TAB_B union select * from TAB_C)a left join (select * from spark_odc_dwd.D_ENS_GMAP_MM where p_hour='#{time yyyyMMddHH}') b on a.IMSI=b.IMSI left join TAB_D c on a.IMSI=c.calling_imsi left join TAB_E d on a.IMSI=d.called_imsi group by a.IMSI, a.GJ, a.YYS

2023-06-07 上传

SELECT ak.dept_id deptId,ak.dept_name deptName,ak.parent_id parentId, IFNULL(pk.primaryShould,0) primaryShould,IFNULL(pk.primaryNot,0) primaryNot,IFNULL(pk.primaryGraduated,0) primaryGraduated, IFNULL(mk.middleShould,0) middleShould,IFNULL(mk.middleNot,0) middleNot,IFNULL(mk.middleGraduated,0) middleGraduated, IFNULL(hk.highShould,0) highShould,IFNULL(hk.highGraduated,0) highGraduated,IFNULL(hk.highNot,0) highNot FROM sys_dept ak LEFT JOIN (SELECT subordinate_school, SUM(CASE WHEN grade = 6 THEN 1 ELSE 0 END) primaryShould, SUM(CASE WHEN grade = 6 AND is_graduation = 'N' THEN 1 ELSE 0 END) primaryNot, SUM(CASE WHEN grade = 6 AND is_graduation = 'Y' THEN 1 ELSE 0 END) primaryGraduated FROM primary_school_roll WHERE primary_student_id LIKE concat('20', '%') GROUP BY subordinate_school) pk ON ak.dept_id = pk.subordinate_school LEFT JOIN (SELECT subordinate_school, SUM(CASE WHEN grade = 9 THEN 1 ELSE 0 END) middleShould, SUM(CASE WHEN grade = 9 AND is_graduation = 'N' THEN 1 ELSE 0 END) middleNot, SUM(CASE WHEN grade = 9 AND is_graduation = 'Y' THEN 1 ELSE 0 END) middleGraduated FROM middle_school_roll WHERE middle_student_id LIKE concat('20', '%') GROUP BY subordinate_school) mk ON ak.dept_id = mk.subordinate_school LEFT JOIN (SELECT subordinate_school, SUM(CASE WHEN grade = 12 THEN 1 ELSE 0 END) highShould, SUM(CASE WHEN graduation_way = '001' AND is_graduation = 'T' THEN 1 ELSE 0 END) highNot, SUM(CASE WHEN graduation_way = '000' AND is_graduation = 'Y' THEN 1 ELSE 0 END) highGraduated FROM high_school_roll WHERE high_student_id LIKE concat('20', '%') GROUP BY subordinate_school) hk ON ak.dept_id = hk.subordinate_school WHERE ak.state = 3 GROUP BY ak.dept_id

2023-06-02 上传