美文网首页
lodash好用方法

lodash好用方法

作者: Biao_349d | 来源:发表于2019-04-11 01:58 被阅读0次
    1. _.compact(array)
      

    创建一个新数组,包含原数组中所有的非假值元素。例如false, null, 0, "", undefined, 和 NaN 都是被认为是“假值”

    _.compact([0, 1, false, 2, '', 3]);
    // => [1, 2, 3]
    
    1. _.difference(array, [values])
      过滤指定值
    _.difference([3, 2, 1], [4, 2]);
    // => [3, 1]
    
    1. .differenceBy(array, [values], [iteratee=.identity])

    过滤数组内对象的值

    // The `_.property` iteratee shorthand.
    _.differenceBy([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], 'x');
    // => [{ 'x': 2 }]
    
    1. .findIndex(array, [predicate=.identity], [fromIndex=0])

    查找数组内的特定对象中在数组内的下标;

    var users = [
      { 'user': 'barney',  'active': false },
      { 'user': 'fred',    'active': false },
      { 'user': 'pebbles', 'active': true }
    ];
     
    _.findIndex(users, function(o) { return o.user == 'barney'; });
    // => 0
     
    // The `_.matches` iteratee shorthand.
    _.findIndex(users, { 'user': 'fred', 'active': false });
    // => 1
     
    // The `_.matchesProperty` iteratee shorthand.
    _.findIndex(users, ['active', false]);
    // => 0
     
    // The `_.property` iteratee shorthand.
    _.findIndex(users, 'active');
    // => 2
    
    1. _.flattenDeep(array)
      深递归去除数组嵌套
    _.flattenDeep([1, [2, [3, [4]], 5]]);
    // => [1, 2, 3, 4, 5]
    
    1. _.uniq(array)
      数组去重
    _.uniq([2, 1, 2]);
    // => [2, 1]
    
    1. .uniqBy(array, [iteratee=.identity])
      数组内对象去重
    // The `_.property` iteratee shorthand.
    _.uniqBy([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
    // => [{ 'x': 1 }, { 'x': 2 }]
    
    1. _.includes(collection, value, [fromIndex=0])
      查找数组内是否包含指定值
      参数
      collection (Array|Object|string): 要检索的集合。
      value (*): 要检索的值。
      [fromIndex=0] (number): 要检索的 索引位置。
    _.includes([1, 2, 3], 1);
    // => true
     
    _.includes([1, 2, 3], 1, 2);
    // => false
     
    _.includes({ 'user': 'fred', 'age': 40 }, 'fred');
    // => true
     
    _.includes('pebbles', 'eb');
    // => true
    
    1. _.sample(collection)
      从collection(集合)中获得一个随机元素。
    _.sample([1, 2, 3, 4]);
    // => 2
    
    1. _.cloneDeep(value)
      对象深拷贝
    var objects = [{ 'a': 1 }, { 'b': 2 }];
     
    var deep = _.cloneDeep(objects);
    console.log(deep[0] === objects[0]);
    // => false
    
    1. _.isArray(value)
      检查 value 是否是 Array 类对象。
    .isArray([1, 2, 3]);
    // => true
    _.isArray(document.body.children);
    // => false
    
    1. _.isBoolean(value)
      检查 value 是否是原始 boolean 类型或者对象。
    _.isBoolean(false);
    // => true
     
    _.isBoolean(null);
    // => false
    
    1. _.isDate(value)
      检查 value 是否是 Date 对象。
    _.isDate(new Date); 
    // => true
     
    _.isDate('Mon April 23 2012');
    // => false
    
    1. _.isElement(value)
      检查 value 是否是可能是 DOM 元素。
    _.isElement(document.body);
    // => true
     
    _.isElement('<body>');
    // => false
    
    1. _.isFunction(value)
      检查 value 是否是 Function 对象。
    _.isFunction(_);
    // => true
     
    _.isFunction(/abc/);
    // => false
    
    1. _.isNil(value)

    检查 value 是否是 null 或者 undefined。

    _.isNil(null);
    // => true
     
    _.isNil(void 0);
    // => true
     
    _.isNil(NaN);
    // => false
    
    1. _.isNumber(value)
      检查 value 是否是原始Number数值型 或者 对象。
    _.isNumber(3);
    // => true
     
    _.isNumber(Number.MIN_VALUE);
    // => true
     
    _.isNumber(Infinity);
    // => true
     
    _.isNumber('3');
    // => false
    
    1. _.isObject(value)
    _.isObject({});
    // => true
     
    _.isObject([1, 2, 3]);
    // => true
     
    _.isObject(_.noop);
    // => true
     
    _.isObject(null);
    // => false
    
    1. _.isString(value)
      检查 value 是否是原始字符串String或者对象。
    _.isString('abc');
    // => true
     
    _.isString(1);
    // => false
    
    1. _.get(object, path, [defaultValue])

    根据 object对象的path路径获取值。 如果解析 value 是 undefined 会以 defaultValue 取代。
    参数
    object (Object): 要检索的对象。
    path (Array|string): 要获取属性的路径。
    [defaultValue] (*): 如果解析值是 undefined ,这值会被返回。

    var object = { 'a': [{ 'b': { 'c': 3 } }] };
     
    _.get(object, 'a[0].b.c');
    // => 3
     
    _.get(object, ['a', '0', 'b', 'c']);
    // => 3
     
    _.get(object, 'a.b.c', 'default');
    // => 'default'
    
    1. _.has(object, path)
      检查 path 是否是object对象的直接属性。
    var object = { 'a': { 'b': 2 } };
    var other = _.create({ 'a': _.create({ 'b': 2 }) });
     
    _.has(object, 'a');
    // => true
     
    _.has(object, 'a.b');
    // => true
     
    _.has(object, ['a', 'b']);
    // => true
     
    _.has(other, 'a');
    // => false
    
    1. _.camelCase([string=''])
      转换字符串string驼峰写法
    _.camelCase('Foo Bar');
    // => 'fooBar'
     
    _.camelCase('--foo-bar--');
    // => 'fooBar'
     
    _.camelCase('__FOO_BAR__');
    // => 'fooBar'
    
    1. _.escapeRegExp([string=''])

    转义 RegExp 字符串中特殊的字符 "^", "$", "", ".", "*", "+", "?", "(", ")", "[", "]", "{", "}", 和 "|" in .

    _.escapeRegExp('[lodash](https://lodash.com/)');
    // => '\[lodash\]\(https://lodash\.com/\)'
    
    1. _.template([string=''], [options={}])
      创建一个预编译模板方法,
    // 使用 "interpolate" 分隔符创建编译模板
    var compiled = _.template('hello <%= user %>!');
    compiled({ 'user': 'fred' });
    // => 'hello fred!'
     
    // 使用 HTML "escape" 转义数据的值
    var compiled = _.template('<b><%- value %></b>');
    compiled({ 'value': '<script>' });
    // => '<b>&lt;script&gt;</b>'
     
    // 使用 "evaluate" 分隔符执行 JavaScript 和 生成HTML代码
    var compiled = _.template('<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>');
    compiled({ 'users': ['fred', 'barney'] });
    // => '<li>fred</li><li>barney</li>'
     
    // 在 "evaluate" 分隔符中使用内部的 `print` 函数
    var compiled = _.template('<% print("hello " + user); %>!');
    compiled({ 'user': 'barney' });
    // => 'hello barney!'
     
    // 使用 ES 分隔符代替默认的 "interpolate" 分隔符
    var compiled = _.template('hello ${ user }!');
    compiled({ 'user': 'pebbles' });
    // => 'hello pebbles!'
     
    // 使用自定义的模板分隔符
    _.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
    var compiled = _.template('hello {{ user }}!');
    compiled({ 'user': 'mustache' });
    // => 'hello mustache!'
     
    // 使用反斜杠符号作为纯文本处理
    var compiled = _.template('<%= "\\<%- value %\\>" %>');
    compiled({ 'value': 'ignored' });
    // => '<%- value %>'
     
    // 使用 `imports` 选项导入 `jq` 作为 `jQuery` 的别名
    var text = '<% jq.each(users, function(user) { %><li><%- user %></li><% }); %>';
    var compiled = _.template(text, { 'imports': { 'jq': jQuery } });
    compiled({ 'users': ['fred', 'barney'] });
    // => '<li>fred</li><li>barney</li>'
     
    // 使用 `sourceURL` 选项指定模板的来源URL
    var compiled = _.template('hello <%= user %>!', { 'sourceURL': '/basic/greeting.jst' });
    compiled(data);
    // => 在开发工具的 Sources 选项卡 或 Resources 面板中找到 "greeting.jst"
     
    // 使用 `variable` 选项确保在编译模板中不声明变量
    var compiled = _.template('hi <%= data.user %>!', { 'variable': 'data' });
    compiled.source;
    // => function(data) {
    //   var __t, __p = '';
    //   __p += 'hi ' + ((__t = ( data.user )) == null ? '' : __t) + '!';
    //   return __p;
    // }
     
    // 使用 `source` 特性内联编译模板
    // 便以查看行号、错误信息、堆栈
    fs.writeFileSync(path.join(cwd, 'jst.js'), '\
      var JST = {\
        "main": ' + _.template(mainText).source + '\
      };\
    ');
    

    25: 过滤指定对象
    过滤两个数组内对象指定key内容相同的值。
    差集

    // The `_.property` iteratee shorthand.
    _.differenceBy([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], 'x');
    // => [{ 'x': 2 }]
    

    26: 交集

    _.intersection([2, 1], [4, 2], [1, 2]);
    // => [2]
    

    相关文章

      网友评论

          本文标题:lodash好用方法

          本文链接:https://www.haomeiwen.com/subject/vetziqtx.html