Lodash工具库Lang学习

    技术2022-07-10  133

    Lang

    一、Lang1_.castArray(value)2_.clone(value)3_.conformsTo(object, source)4_.eq(value, other)5_.gt(value, other)6_.isArray(value)7_.isObject(value)8_.toArray(value)

    一、Lang

    1_.castArray(value)

    如果 value 不是数组, 那么强制转为数组。

    console.log(_.castArray({'age':18})); console.log(_.castArray('age'));

    2_.clone(value)

    创建一个 value 的浅拷贝。(拷贝引用值时原数据改,备份数据也改) _.cloneDeep(value) 深拷贝(拷贝引用值时原数据改,备份数据不改)

    3_.conformsTo(object, source)

    通过调用断言source的属性与 object 的相应属性值,检查 object是否符合 source。当source偏应用时,这种方法和 _.conforms函数是等价的。

    var obj = {'name':'jimo','age':18}; console.log(_.conformsTo(obj,{'age':function (n){return n>17}})); //选择出obj.age>17

    4_.eq(value, other)

    执行 SameValueZero 比较两者的值,来确定它们是否相等。

    var object = { 'a': 1 }; var other = { 'a': 1 }; _.eq(object, object);//true _.eq(object, other);//false _.eq(NaN,NaN);//true //引用值地址不一样

    5_.gt(value, other)

    检查 value是否大于 other。 _.gte(value, other) 检查 value是否大于或者等于 other。

    6_.isArray(value)

    检查 value 是否是 Array 类对象。

    _.isArray(document.body.children);//是个类数组false

    7_.isObject(value)

    检查 value 是否为 Object 的 language type。 (例如: arrays, functions, objects, regexes,new Number(0), 以及 new String(’’))

    _.isObject(null);//false //这里更正历史遗留问题

    8_.toArray(value)

    转换 value 为一个数组。

    _.toArray({ 'a': 1, 'b': 2 });//[1, 2] _.toArray('abc');// => ['a', 'b', 'c'] //和前面的castArray相对温和 //例如null转换不了则返回[]
    Processed: 0.010, SQL: 9