JS去重函数的扩展应用
2016-08-10 21:58
363 查看
数据:
账单id[1,1,1,1,2,2,2,3,3,3,3,3,3,3],
相对于账单id的金额[100,120,110,150,200,180,220,115,150,120,180,220,140,200],
要求:
去重账单id数组,按id分组账单金额总额数组,按账单分组账单金额明细(二维数组)。
JS方法实现:(该方法的前提是id与金额一一对应,且id已经排序)
无序id数组的实现,示例代码如下:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<script type="text/javascript">
var arrId =[1,1,2,1,3,1,2,3,3,3,2,3,3,3];
var arrAmount = [100,120,110,150,200,180,220,115,150,120,180,220,140,200];
var tmpRes = uniqueArray(arrId,arrAmount);
alert(tmpRes[0]+'----'+tmpRes[1]+'----'+tmpRes[2][0]+'||'+tmpRes[2][1]+'||'+tmpRes[2][2]);
function uniqueArray(arrId,arrAmount){
var ids = new Array(); //存放id新数组
var amount = new Array();//存放金额新数组
var groupAmount = new Array();//分组金额数组
var res =[ids,amount,groupAmount];//结果数组
var json = {};
var index =0;
for(var i = 0; i < arrId.length; i++){
if(!json[arrId[i]]){//不重复操作
ids.push(arrId[i]);
amount.push(arrAmount[i]);
json[arrId[i]] = 1;
groupAmount[index]=new Array();//声明二维数组
groupAmount[index].push(arrAmount[i]);
index++;
}else{ //重复时操作
//求arrId[i]的值位于ids的位置--这样才保证无序也可以实现
var ins = getIndexofOb(ids,arrId[i]);
// amount[index-1] = parseFloat(amount[index-1])+ parseFloat(arrAmount[i]);
// groupAmount[index-1].push(arrAmount[i]);
amount[ins] = parseFloat(amount[ins])+ parseFloat(arrAmount[i]);
groupAmount[ins].push(arrAmount[i]);
}
}
return res;
}
/**
* 返回ob在arrOb中索引值,若不存在,返回-1
* @param arrOb
* @param ob
* @returns {number}
*/
function getIndexofOb(arrOb,ob){
for(var i= 0;i<arrOb.length;i++){
if(ob==arrOb[i]){
return i;
}
}
return -1;
}
</script>
<body>
</body>
</html>
账单id[1,1,1,1,2,2,2,3,3,3,3,3,3,3],
相对于账单id的金额[100,120,110,150,200,180,220,115,150,120,180,220,140,200],
要求:
去重账单id数组,按id分组账单金额总额数组,按账单分组账单金额明细(二维数组)。
JS方法实现:(该方法的前提是id与金额一一对应,且id已经排序)
/** * 重构公司id和金额数组 * @param arrId:companyId数组 * @param arrAmount:金额数组 * @returns ids:去重后的id数组 * @returns amount:按id分组求和的金额数组 * @returns groupAmount:按id分组的二位金额数组 */ function uniqueArray(arrId,arrAmount){ var ids = new Array(); //存放id新数组 var amount = new Array();//存放金额新数组 var groupAmount = new Array();//分组金额数组 var res =[ids,amount,groupAmount];//结果数组 var json = {}; var index =0; for(var i = 0; i < arrId.length; i++){ if(!json[arrId[i]]){//不重复操作 ids.push(arrId[i]); amount.push(arrAmount[i]); json[arrId[i]] = 1; groupAmount[index]=new Array();//声明二维数组 groupAmount[index].push(arrAmount[i]); index++; }else{ //重复时操作 amount[index-1] = parseFloat(amount[index-1])+ parseFloat(arrAmount[i]); groupAmount[index-1].push(arrAmount[i]); } } return res; }无序id的实现后续。。。
无序id数组的实现,示例代码如下:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<script type="text/javascript">
var arrId =[1,1,2,1,3,1,2,3,3,3,2,3,3,3];
var arrAmount = [100,120,110,150,200,180,220,115,150,120,180,220,140,200];
var tmpRes = uniqueArray(arrId,arrAmount);
alert(tmpRes[0]+'----'+tmpRes[1]+'----'+tmpRes[2][0]+'||'+tmpRes[2][1]+'||'+tmpRes[2][2]);
function uniqueArray(arrId,arrAmount){
var ids = new Array(); //存放id新数组
var amount = new Array();//存放金额新数组
var groupAmount = new Array();//分组金额数组
var res =[ids,amount,groupAmount];//结果数组
var json = {};
var index =0;
for(var i = 0; i < arrId.length; i++){
if(!json[arrId[i]]){//不重复操作
ids.push(arrId[i]);
amount.push(arrAmount[i]);
json[arrId[i]] = 1;
groupAmount[index]=new Array();//声明二维数组
groupAmount[index].push(arrAmount[i]);
index++;
}else{ //重复时操作
//求arrId[i]的值位于ids的位置--这样才保证无序也可以实现
var ins = getIndexofOb(ids,arrId[i]);
// amount[index-1] = parseFloat(amount[index-1])+ parseFloat(arrAmount[i]);
// groupAmount[index-1].push(arrAmount[i]);
amount[ins] = parseFloat(amount[ins])+ parseFloat(arrAmount[i]);
groupAmount[ins].push(arrAmount[i]);
}
}
return res;
}
/**
* 返回ob在arrOb中索引值,若不存在,返回-1
* @param arrOb
* @param ob
* @returns {number}
*/
function getIndexofOb(arrOb,ob){
for(var i= 0;i<arrOb.length;i++){
if(ob==arrOb[i]){
return i;
}
}
return -1;
}
</script>
<body>
</body>
</html>
相关文章推荐
- js 动态添加标签(新增一行,其实很简单,就是几个函数的应用)
- OpenLayers BaseType.js扩展操作函数(六)
- js获取时间函数扩展
- js setTimeout()函数介绍及应用以倒计时为例
- 汇总近期Js函数扩展
- prototype.js日常应用函数积累
- Js中常用的字符串,数组,函数扩展
- js字符串常用函数及扩展总结
- js中eval()函数和trim()去掉字符串左右空格应用
- JS基本函数扩展
- JS对时间的相关处理函数与应用
- JS里的prototype对JS类(方法,函数,function)进行一个扩展吧!
- js radio应用中出错调试:原来js函数写错整个函数都不执行的
- IOCP以及扩展Windows socket函数应用示例
- WebView使用总结(应用函数与JS函数互相调用)
- 模仿JQuery.extend函数扩展自己对象的js代码
- 模仿JQuery.extend函数扩展自己对象的js代码
- js获取url及起参数列表及扩展应用
- js 时间函数应用。加 减 比较 格式转换
- WebView使用总结(应用函数与JS函数互相调用)