underscore的新数组应用

 前几天有个小需求 根据一个大数组得到一个权限的数组并且新增的数组是其中某几项相加 相减的记过  查询了下

underscore 文档 很方便了实现了功能 如下

var _=require('underscore') ;
var a=[{one: 1, two: 2, three: 3},{one: 1, two: 2, three: 3},{one: 1, two: 2, three: 3}]
var ret=_.map(a,
    function(num, key){
        num.d=[num.one,num.two] //新数组增加的
        return num ;
    });
console.log(JSON.stringify(ret))


结果如下

/usr/local/bin/node un.js
[
{"one":1,"two":2,"three":3,"d":[1,2]},
{"one":1,"two":2,"three":3,"d":[1,2]},
{"one":1,"two":2,"three":3,"d":[1,2]}
]