RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 617963
Accepted
Эдуард
Эдуард
Asked:2020-01-22 21:22:20 +0000 UTC2020-01-22 21:22:20 +0000 UTC 2020-01-22 21:22:20 +0000 UTC

现在需要下划线吗?

  • 772

您使用下划线的哪些功能?例如,它具有 each 功能。如果现在在常规 js 中有 forEach, map 为什么需要它?我知道这个库在没有 ES5 的时候是相关的吗?

javascript
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    spectre_it
    2020-01-22T21:51:29Z2020-01-22T21:51:29Z

    不,不需要,您可以使用本地语言工具来完成。对图书馆的兴趣正在逐渐下降。以下是 Google 请求的动态:

    在此处输入图像描述

    自己比较:

    数组

    _.concat

    // Underscore/Lodash
    var array = [1]
    var other = _.concat(array, 2, [3], [
      [4]
    ])
    
    console.log(other)
      // результат: [1, 2, 3, [4]]
    
    // Native
    var array = [1]
    var other = array.concat(2, [3], [
      [4]
    ])
    
    console.log(other)
      // результат: [1, 2, 3, [4]]
    

    _。充满

    // Underscore/Lodash
    var array = [1, 2, 3]
    
    _.fill(array, 'a')
    
    console.log(array)
      // результат: ['a', 'a', 'a']
    
    _.fill(Array(3), 2)
      // результат: [2, 2, 2]
    
    _.fill([4, 6, 8, 10], '*', 1, 3)
      // результат: [4, '*', '*', 10]
    
    // Native
    var array = [1, 2, 3]
    
    array.fill('a')
    
    console.log(array)
      // результат: ['a', 'a', 'a']
    
    Array(3).fill(2)
      // результат: [2, 2, 2]
    
    [4, 6, 8, 10].fill('*', 1, 3)
      // результат: [4, '*', '*', 10]
    

    _。寻找

    // Underscore/Lodash
    var users = [{
      'user': 'barney',
      'age': 36,
      'active': true
    }, {
      'user': 'fred',
      'age': 40,
      'active': false
    }, {
      'user': 'pebbles',
      'age': 1,
      'active': true
    }]
    
    _.find(users, function(o) {
        return o.age < 40;
      })
      // результат: object for 'barney'
    
    // Native
    var users = [{
      'user': 'barney',
      'age': 36,
      'active': true
    }, {
      'user': 'fred',
      'age': 40,
      'active': false
    }, {
      'user': 'pebbles',
      'age': 1,
      'active': true
    }]
    
    users.find(function(o) {
        return o.age < 40;
      })
      // результат: object for 'barney'
    

    _.findIndex

    // Underscore/Lodash
    var users = [{
      'user': 'barney',
      'age': 36,
      'active': true
    }, {
      'user': 'fred',
      'age': 40,
      'active': false
    }, {
      'user': 'pebbles',
      'age': 1,
      'active': true
    }]
    
    var index = _.findIndex(users, function(o) {
      return o.age >= 40;
    })
    console.log(index)
      // результат: 1
    
    // Native
    var users = [{
      'user': 'barney',
      'age': 36,
      'active': true
    }, {
      'user': 'fred',
      'age': 40,
      'active': false
    }, {
      'user': 'pebbles',
      'age': 1,
      'active': true
    }]
    
    var index = users.findIndex(function(o) {
      return o.age >= 40;
    })
    console.log(index)
      // результат: 1
    

    _。指数

    // Underscore/Lodash
    var array = [2, 9, 9]
    var result = _.indexOf(array, 2)
    console.log(result)
      // результат: 0
    
    // Native
    var array = [2, 9, 9]
    var result = array.indexOf(2)
    console.log(result)
      // результат: 0
    

    _。加入

    // Lodash
    var result = _.join(['one', 'two', 'three'], '--')
    console.log(result)
      // результат: 'one--two--three'
    
    // Native
    var result = ['one', 'two', 'three'].join('--')
    console.log(result)
      // результат: 'one--two--three'
    

    _.lastIndexOf

    // Underscore/Lodash
    var array = [2, 9, 9, 4, 3, 6]
    var result = _.lastIndexOf(array, 9)
    console.log(result)
      // результат: 2
    
    // Native
    var array = [2, 9, 9, 4, 3, 6]
    var result = array.lastIndexOf(9)
    console.log(result)
      // результат: 2
    

    _。撤销

    // Lodash
    var array = [1, 2, 3]
    console.log(_.reverse(array))
      // результат: [3, 2, 1]
    
    // Native
    var array = [1, 2, 3]
    console.log(array.reverse())
      // результат: [3, 2, 1]
    

    收藏品

    _。每个

    // Underscore/Lodash
    _.each([1, 2, 3], function(value, index) {
        console.log(value)
      })
      // результат: 1 2 3
    
    // Native
    [1, 2, 3].forEach(function(value, index) {
        console.log(value)
      })
      // результат: 1 2 3
    

    _。每一个

    // Underscore/Lodash
    function isLargerThanTen(element, index, array) {
      return element >= 10
    }
    var array = [10, 20, 30]
    var result = _.every(array, isLargerThanTen)
    console.log(result)
      // результат: true
    
    // Native
    function isLargerThanTen(element, index, array) {
      return element >= 10
    }
    
    var array = [10, 20, 30]
    var result = array.every(isLargerThanTen)
    console.log(result)
      // результат: true
    

    _。筛选

    // Underscore/Lodash
    function isBigEnough(value) {
      return value >= 10
    }
    var array = [12, 5, 8, 130, 44]
    var filtered = _.filter(array, isBigEnough)
    console.log(filtered)
      // результат: [12, 130, 44]
    
    // Native
    function isBigEnough(value) {
      return value >= 10
    }
    var array = [12, 5, 8, 130, 44]
    var filtered = array.filter(isBigEnough)
    console.log(filtered)
      // результат: [12, 130, 44]
    

    _.包括

    var array = [1, 2, 3]
      // Underscore/Lodash - also called with _.contains
    _.includes(array, 1)
      // результат: true
    
    // Native
    var array = [1, 2, 3]
    array.includes(1)
      // результат: true
    
    // Native (only works with flat array values, no complex objects)
    var array = [1, 2, 3]
    array.indexOf(1) > -1
      // результат: true
    

    _。地图

    // Underscore/Lodash
    var array1 = [1, 2, 3]
    var array2 = _.map(array1, function(value, index) {
      return value * 2
    })
    console.log(array2)
      // результат: [2, 4, 6]
    
    // Native
    var array1 = [1, 2, 3]
    var array2 = array1.map(function(value, index) {
      return value * 2
    })
    console.log(array2)
      // результат: [2, 4, 6]
    

    _。采摘

    // Underscore/Lodash
    var array1 = [{
      name: "Alice"
    }, {
      name: "Bob"
    }, {
      name: "Jeremy"
    }]
    var names = _.pluck(array1, "name")
    console.log(names)
      // результат: ["Alice", "Bob", "Jeremy"]
    
    // Native
    var array1 = [{
      name: "Alice"
    }, {
      name: "Bob"
    }, {
      name: "Jeremy"
    }]
    var names = array1.map(function(x) {
      return x.name
    })
    console.log(names)
      // результат: ["Alice", "Bob", "Jeremy"]
    

    _。减少

    // Underscore/Lodash
    var array = [0, 1, 2, 3, 4]
    var result = _.reduce(array, function(previousValue, currentValue, currentIndex, array) {
      return previousValue + currentValue
    })
    console.log(result)
      // результат: 10
    
    // Native
    var array = [0, 1, 2, 3, 4]
    var result = array.reduce(function(previousValue, currentValue, currentIndex, array) {
      return previousValue + currentValue
    })
    console.log(result)
      // результат: 10
    

    _.reduceRight

    // Underscore/Lodash
    var array = [0, 1, 2, 3, 4]
    var result = _.reduceRight(array, function(previousValue, currentValue, currentIndex, array) {
      return previousValue - currentValue
    })
    console.log(result)
      // результат: -2
    
    // Native
    var array = [0, 1, 2, 3, 4]
    var result = array.reduceRight(function(previousValue, currentValue, currentIndex, array) {
      return previousValue - currentValue
    })
    console.log(result)
      // результат: -2
    

    _。尺寸

    // Underscore/Lodash
    var result = _.size({
      one: 1,
      two: 2,
      three: 3
    })
    console.log(result)
      // результат: 3
    
    // Native
    var result2 = Object.keys({
      one: 1,
      two: 2,
      three: 3
    }).length
    console.log(result2)
      // результат: 3
    

    _。一些

    // Underscore/Lodash
    function isLargerThanTen(element, index, array) {
      return element >= 10
    }
    var array = [10, 9, 8]
    var result = _.some(array, isLargerThanTen)
    console.log(result)
      // результат: true
    
    // Native
    function isLargerThanTen(element, index, array) {
      return element >= 10
    }
    
    var array = [10, 9, 8]
    var result = array.some(isLargerThanTen)
    console.log(result)
      // результат: true
    

    功能

    _。后

    var notes = ['profile', 'settings']
      // Underscore/Lodash
    var renderNotes = _.after(notes.length, render)
    notes.forEach(function(note) {
      console.log(note)
      renderNotes()
    })
    
    // Native
    notes.forEach(function(note, index) {
      console.log(note)
      if (notes.length === (index + 1)) {
        render()
      }
    })
    

    语

    _.isNaN

    // Underscore/Lodash
    console.log(_.isNaN(NaN))
      // результат: true
    
    // Native
    console.log(isNaN(NaN))
      // результат: true
    
    // ES6
    console.log(Number.isNaN(NaN))
      // результат: true
    

    一个东西

    _。分配

    // Underscore: _.extendOwn
    // Lodash
    function Foo() {
      this.c = 3;
    }
    
    function Bar() {
      this.e = 5;
    }
    Foo.prototype.d = 4;
    Bar.prototype.f = 6;
    var result = _.assign(new Foo, new Bar);
    console.log(result);
    // результат: { 'c': 3, 'e': 5 }
    
    // Native
    function Foo() {
      this.c = 3;
    }
    
    function Bar() {
      this.e = 5;
    }
    Foo.prototype.d = 4;
    Bar.prototype.f = 6;
    var result = Object.assign(new Foo, new Bar);
    console.log(result);
    // результат: { 'c': 3, 'e': 5 }
    

    _.键

    // Underscore/Lodash
    var result = _.keys({
      one: 1,
      two: 2,
      three: 3
    })
    console.log(result)
      // результат: ["one", "two", "three"]
    
    // Native
    var result2 = Object.keys({
      one: 1,
      two: 2,
      three: 3
    })
    console.log(result2)
      // результат: ["one", "two", "three"]
    

    线

    _。重复

    // Lodash
    var result = _.repeat('abc', 2)
      // результат: 'abcabc'
    
    // Native
    var result = 'abc'.repeat(2)
    console.log(result)
      // результат: 'abcabc'
    

    _。降低

    // Lodash
    var result = _.toLower('FOOBAR')
    console.log(result)
      // результат: 'foobar'
    
    // Native
    var result = 'FOOBAR'.toLowerCase()
    console.log(result)
      // результат: 'foobar'
    

    _.toUpper

    // Lodash
    var result = _.toUpper('foobar')
    console.log(result)
      // результат: 'FOOBAR'
    
    // Native
    var result = 'foobar'.toUpperCase()
    console.log(result)
      // результат: 'FOOBAR'
    

    _。修剪

    // Lodash
    var result = _.trim(' abc ')
    console.log(result)
      // результат: 'abc'
    
    // Native
    var result = ' abc '.trim()
    console.log(result)
      // результат: 'abc'
    

    链接

    • 原来的
    • Mozilla 开发者网络
    • 下划线.js
    • Lodash.js
    • 你 - 不需要 - jQuery
    • 12

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    Python 3.6 - 安装 MySQL (Windows)

    • 1 个回答
  • Marko Smith

    C++ 编写程序“计算单个岛屿”。填充一个二维数组 12x12 0 和 1

    • 2 个回答
  • Marko Smith

    返回指针的函数

    • 1 个回答
  • Marko Smith

    我使用 django 管理面板添加图像,但它没有显示

    • 1 个回答
  • Marko Smith

    这些条目是什么意思,它们的完整等效项是什么样的

    • 2 个回答
  • Marko Smith

    浏览器仍然缓存文件数据

    • 1 个回答
  • Marko Smith

    在 Excel VBA 中激活工作表的问题

    • 3 个回答
  • Marko Smith

    为什么内置类型中包含复数而小数不包含?

    • 2 个回答
  • Marko Smith

    获得唯一途径

    • 3 个回答
  • Marko Smith

    告诉我一个像幻灯片一样创建滚动的库

    • 1 个回答
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Алексей Шиманский 如何以及通过什么方式来查找 Javascript 代码中的错误? 2020-08-03 00:21:37 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5