(三)underscore.js框架Objects类API学习

_.keys(object)
Retrieve all the names of the object‘s properties.

_.keys({one: 1, two: 2, three: 3});
=> ["one", "two", "three"]

_.values(object)
Return all of the values of the object’s properties.

_.values({one: 1, two: 2, three: 3});
=> [1, 2, 3]

_.pairs(object)
Convert an object into a list of [key, value] pairs.

_.pairs({one: 1, two: 2, three: 3});
=> [["one", 1], ["two", 2], ["three", 3]]

_.invert(object)
Returns a copy of the object where the keys have become the values and the values the keys. For this to work, all of your object’s values should be unique and string serializable.

_.invert({Moe: "Moses", Larry: "Louis", Curly: "Jerome"});
=> {Moses: "Moe", Louis: "Larry", Jerome: "Curly"};

_.functions(object) Alias: methods
Returns a sorted list of the names of every method in an object — that is to say, the name of every function property of the object.

_.functions(_);
=> ["all", "any", "bind", "bindAll", "clone", "compact", "compose" ...

_.extend(destination,
*sources)

Copy all of the properties in the source objects over to the destination object, and return the destination object. It’s in-order, so the last source will override properties of the same name in previous arguments.

_.extend({name: 'moe'}, {age: 50});
=> {name: 'moe', age: 50}
var destination = {name: 'moe'};
var source = {age: 50}
_.extend(destination, source);
console.log("extend="+destination.age);//50

_.pick(object,
*keys)

Return a copy of the object, filtered to only have values for the whitelisted(白名单) keys (or array of valid keys). Alternatively accepts a predicate indicating which keys to pick.

_.pick({name: 'moe', age: 50, userid: 'moe1'}, 'name', 'age');
=> {name: 'moe', age: 50}
_.pick({name: 'moe', age: 50, userid: 'moe1'}, function(value, key, object) {
  return _.isNumber(value);
});
=> {age: 50}

_.omit(object,
*keys)

Return a copy of the object, filtered to omit the blacklisted(黑名单) keys (or array of keys). Alternatively accepts a predicate indicating which keys to omit.

_.omit({name: 'moe', age: 50, userid: 'moe1'}, 'userid');
=> {name: 'moe', age: 50}
_.omit({name: 'moe', age: 50, userid: 'moe1'}, function(value, key, object) {
  return _.isNumber(value);
});
=> {name: 'moe', userid: 'moe1'}

_.defaults(object,
*defaults)

Fill in undefined properties in object with the first value present in the following list of defaults objects.

var iceCream = {flavor: "chocolate"};
_.defaults(iceCream, {flavor: "vanilla", sprinkles: "lots"});
=> {flavor: "chocolate", sprinkles: "lots"}

说明:这个函数和extend非常类似,假设destination和source中属性名没有反复,那么2个函数的功能是全然一致的。

区别在于:当属性名有同名的时候,extend直接用source中的值覆盖掉destination中的值;而defaults则会依据destination中的属性值是否为undefined区别对待。

var iceCream = {flavor: "chocolate",sprinkles:undefined};
_.defaults(iceCream, {flavor: "vanilla", sprinkles: "lots"});
console.log("iceCream=" + iceCream.flavor);//chocolate
console.log("sprinkles=" + iceCream.sprinkles);//lots

_.clone(object)
Create a shallow-copied(浅拷贝) clone of the object. Any nested objects or arrays will be copied by reference, not duplicated.

_.clone({name: 'moe'});
=> {name: 'moe'};

_.tap(object,
interceptor)

Invokes interceptor with the object, and then returns object. The primary purpose of this method is to “tap into” a method chain, in order to perform operations on intermediate results within the chain.

_.chain([1,2,3,200])
  .filter(function(num) { return num % 2 == 0; })
  .tap(alert)
  .map(function(num) { return num * num })
  .value();
=> // [2, 200] (alerted)
=> [4, 40000]

_.has(object,
key)

Does the object contain the given key? Identical to object.hasOwnProperty(key), but uses a safe reference to the hasOwnProperty function, in case it’s been overridden accidentally.

_.has({a: 1, b: 2, c: 3}, "b");
=> true

_.property(key)
Returns a function that will itself return the key property of any passed-in object.

var moe = {name: 'moe'};
'moe' === _.property('name')(moe);
=> true

_.matches(attrs)
Returns a predicate function that will tell you if a passed in object contains all of the key/value properties present in attrs.

var ready = _.matches({selected: true, visible: true});
var readyToGoList = _.filter(list, ready);

_.isEqual(object,
other)

Performs an optimized deep comparison between the two objects, to determine if they should be considered equal.

var moe   = {name: 'moe', luckyNumbers: [13, 27, 34]};
var clone = {name: 'moe', luckyNumbers: [13, 27, 34]};
moe == clone;
=> false
_.isEqual(moe, clone);
=> true

_.isEmpty(object)
Returns true if an enumerable object contains no values (no enumerable own-properties). For strings and array-like objects _.isEmpty checks if the length property is 0.

_.isEmpty([1, 2, 3]);
=> false
_.isEmpty({});
=> true

_.isElement(object)
Returns true if object is a DOM element.

_.isElement(jQuery('body')[0]);
=> true

_.isArray(object)
Returns true if object is an Array.

(function(){ return _.isArray(arguments); })();
=> false
_.isArray([1,2,3]);
=> true

_.isObject(value)
Returns true if value is an Object. Note that JavaScript arrays and functions are objects, while (normal) strings and numbers are not.

_.isObject({});
=> true
_.isObject(1);
=> false

_.isArguments(object)
Returns true if object is an Arguments object.

(function(){ return _.isArguments(arguments); })(1, 2, 3);
=> true
_.isArguments([1,2,3]);
=> false

_.isFunction(object)
Returns true if object is a Function.

_.isFunction(alert);
=> true

_.isString(object)
Returns true if object is a String.

_.isString("moe");
=> true

_.isNumber(object)
Returns true if object is a Number (including NaN).

_.isNumber(8.4 * 5);
=> true

_.isFinite(object)
Returns true if object is a finite Number.

_.isFinite(-101);
=> true

_.isFinite(-Infinity);
=> false

_.isBoolean(object)
Returns true if object is either true or false.

_.isBoolean(null);
=> false

_.isDate(object)
Returns true if object is a Date.

_.isDate(new Date());
=> true

_.isRegExp(object)
Returns true if object is a RegExp.

_.isRegExp(/moe/);
=> true

_.isNaN(object)
Returns true if object is NaN.

Note: this is not the same as the native isNaN function, which will also return true for many other not-number values, such as undefined.

_.isNaN(NaN);
=> true
isNaN(undefined);
=> true
_.isNaN(undefined);
=> false

_.isNull(object)
Returns true if the value of object is null.

_.isNull(null);
=> true
_.isNull(undefined);
=> false

_.isUndefined(value)
Returns true if value is undefined.

_.isUndefined(window.missingVariable);
=> true

Original: https://www.cnblogs.com/cxchanpin/p/7403721.html
Author: cxchanpin
Title: (三)underscore.js框架Objects类API学习

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/551473/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Hexo博客Next v7.X 主题升级,美化警示录

    前言 经历了好几天(懒癌晚期懒得数了)的与主题升级斗争后,我终于完成基本上完成了next主题的升级!升到了V7.3!哈哈哈哈哈哈隔,我一个小白干这事干嘛呀我….这不是自…

    技术杂谈 2023年6月21日
    0118
  • RuntimeError: No response returned.报错分析与解决方案

    这是在做开源项目的时候遇到的问题,程序部署上线后不定时会突然出现这样一条报错,终于被搞烦了决定彻底查清原因。 这是我正在使用的版本 fastapi==0.78.0 uvicorn=…

    技术杂谈 2023年6月21日
    0115
  • 【软考】信息安全审计

    1.概念 安全审计(Security Audit)是记录、审查主体对客体进行访问和使用情况,保证安全规则被正确执行,并帮助分析安全事故产生的原因。 安全审计系统采用数据挖掘和数据仓…

    技术杂谈 2023年5月31日
    0103
  • Exchange配置

    Exchange2019安装前准备: 1.安装.net4.82.安装vcredist3.安装UcmaRuntime4.安装rewrite_amd64_zh-CN 5.准备DAG I…

    技术杂谈 2023年5月31日
    086
  • static关键字的一些使用

    百度百科定义static关键字 通常情况下,类成员必须通过它的类的对象访问,但是可以创建这样一个成员,它能够被它自己使用,而不必引用特定的实例。在成员的声明前面加上关键字stati…

    技术杂谈 2023年7月25日
    077
  • 基于STSdb和fastJson的磁盘/内存缓存

    增加了对批量处理的支持,写操作速度提升5倍,读操作提升100倍 增加了对并发的支持 业务系统用的是数据库,数据量大,部分只读或相对稳定业务查询复杂,每次页面加载都要花耗不少时间(不…

    技术杂谈 2023年5月31日
    080
  • 圆方图(铁人两项)

    #include using namespace std; const int MM=400005; int dfn[MM],low[MM],dfc,cnt,in[MM],tot,…

    技术杂谈 2023年6月21日
    085
  • 使用Aggregated API扩展你的kubernetes API

    404. 抱歉,您访问的资源不存在。 可能是网址有误,或者对应的内容被删除,或者处于私有状态。 代码改变世界,联系邮箱 contact@cnblogs.com 园子的商业化努力-困…

    技术杂谈 2023年7月25日
    075
  • 设计模式-单例模式

    类型:创建型。 目的:杜绝相同对象的反复创建,提升系统性能。 话不多说,直接看实现方案例。 实现案例 项目启动时加载 public class Test { private sta…

    技术杂谈 2023年7月11日
    098
  • 前端性能数据监控统计的探索

    前端性能数据监控统计的探索 https://mp.weixin.qq.com/s/wPQvHAnVI_zZ69UwLVStcg 搜索 复制 Original: https://ww…

    技术杂谈 2023年5月31日
    075
  • 安装apollo

    404. 抱歉,您访问的资源不存在。 可能是网址有误,或者对应的内容被删除,或者处于私有状态。 代码改变世界,联系邮箱 contact@cnblogs.com 园子的商业化努力-困…

    技术杂谈 2023年5月31日
    086
  • go-micro使用Consul做服务发现的方法和原理

    go-micro v4默认使用mdns做服务发现。不过也支持采用其它的服务发现中间件,因为多年来一直使用Consul做服务发现,为了方便和其它服务集成,所以还是选择了Consul。…

    技术杂谈 2023年7月11日
    095
  • 【MAT-MemoryAnalyzer】使用快速排查问题

    【MAT-MemoryAnalyzer】使用快速排查问题 引用地址:https://blog.csdn.net/lyd135364/article/details/12144996…

    技术杂谈 2023年5月30日
    093
  • VIM简单配置

    配置vim配置 编辑配置文件 feng@mint ~ $ vim ~/.vimrc 配置如下主要配置为自动换行,设置行号,设置tab键为4个空格,同时将tab键自动转换成空格 se…

    技术杂谈 2023年6月21日
    081
  • YAML注释

    既然对YAML的语法和基础知识感到满意,那么进一步了解它的细节。在本章中,将了解如何在YAML中使用注释。 YAML支持单行注释。 下面借助一个例子来解释其结构 – Y…

    技术杂谈 2023年5月31日
    093
  • Python去水印方法(无需安装任何库)

    分享一个Python 自带库去水印的方法 今天用WPS将PDF转图片,发现没有会员就会自带水印,于是萌生了用Python去水印的想法 from itertools import p…

    技术杂谈 2023年7月24日
    092
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球