underscorejs-groupBy学习
2016-02-01 20:20
691 查看
2.18 groupBy
2.18.1 语法
_.groupBy(list, iteratee, [context])2.18.2 说明
把list分为多个集合,iterator为分组的依据,返回值为Objectlist可以是数组、对象、字符串或arguments等
iteratee为分组的依据.
iterator的参数(value, key, list)
iterator如果是function需要返回值
context可以改变iterator内部的this
2.18.3 代码示例
示例一:list可以是数组、对象、字符串或arguments等
var parity; var iteratee = function(value, key, list){ return value % 2; // value % 2的结果是0或是1,所以key就是0或是1 //return value % 2 === 0; //这样子就变成了true或false }; //list为数组 parity = _.groupBy([1, 2, 3], iteratee); console.log(parity); //=> {0:[2], 1:[1, 3]} //list为对象 parity = _.groupBy({a:1, b:2, c:3}, iteratee); console.log(parity); //=> {0:[2], 1:[1, 3]} //list为字符串 parity = _.groupBy('123', iteratee); console.log(parity); //=> {0:[2], 1:[1, 3]} //list为arguments (function(){ parity = _.groupBy(arguments, iteratee); console.log(parity); //=> {0:[2], 1:[1, 3]} }(1, 2, 3));
示例二:iteratee可以全局的方法
var parity; //iteratee可以是全局的方法 parity = _.groupBy([1, 1.4, 1.6, 1.9], Math.floor); console.log(parity); //=> {1 : [1, 1.4, 1.6, 1.9]} parity = _.groupBy([1, 1.4, 1.6, 1.9], Math.ceil); console.log(parity); //=> {1 : [1], 2: [1.4, 1.6, 1.9]}
示例三:iteratee可以是list内元素的属性
var parity = _.groupBy(['a', 'b', 'cc'], 'length'); console.log(parity); //=> {1:['a', 'b'], 2:['c']}
示例四:iteratee可以是list内,元素的key
这种情况其实是用的最多的。var array = [{ "type": "stream", "price": "3.99", "id": "13nee" }, { "type": "stream", "price": "2.99", "id": "8ejwj" }, { "type": "buy", "price": "3.99". "id": "9akwk" }]; var parity = _.groupBy(array, 'type'); console.log(parity); //=> // { // stream: [{ // "type": "stream", // "price": "3.99", // "id": "13nee" // }, { // "type": "stream", // "price": "2.99", // "id": "8ejwj" // }], // buy: [{ // "type": "buy", // "price": "3.99". // "id": "9akwk" // }] // }
示例五:iteratee的参数
_.groupBy('abc', function(v, i, l){ console.log(v, i, l); //=> a 0 abc //=> b 1 abc //=> c 2 abc return v; });
示例六:context可以改变iterator内部的this(坑)
_.groupBy('1', function(v, i, l){ console.log(this);//=> Object {txt: "moe"} }, {txt : 'moe'}); _.groupBy('1', function(v, i, l){ console.log(this ===1 ); //true or false? }, 1);
2.18.4 list的特殊情况
console.log(_.groupBy(null)); //=> Object {} console.log(_.groupBy(undefined)); //=> Object {} console.log(_.groupBy(NaN)); //=> Object {} console.log(_.groupBy(true)); //=> Object {} console.log(_.groupBy(false)); //=> Object {}
2.18.5 将下列数组,按是否数字分类
var arr = [1, '1', '2', 2, '3', '3']; var parity = (function(arr){ //写下你的代码 }(arr)); console.log(parity); //=> {false:['1', '2', '3', '3'], true: [1, 2]}
相关文章推荐
- underscorejs-sortBy学习
- 《JavaScript DOM编程艺术》学习
- Javascript学习笔记6——BOM
- javascript实现贪吃蛇
- JS解析Json数据
- C#后台格式化JSON字符串显示
- 【公司动态添加行】前台穿一个json的字符串到后台,并解析
- bug记录:Json Parsing Error : JSON.parse: bad control character in string literal
- 原生JS实现图标图片拖拽
- 二分法查找 --JS 实现
- js功能实现的特效--距离新年还有多少天
- MyEclipse安装JS代码提示(Spket插件)
- js监控键盘 设置快捷键方法
- 拼接字符串去掉最后多余的串,JSON的遍历
- js传值
- ember.js里的实用方法
- sublime text 3 添加 javascript 代码片段 ( snippet )
- MVC的JsonResult用法
- 再谈javascript原型继承
- js获取服务端的头信息