2.13 omit

5.13.1 语法:

_.omit(obj, *args);

2.13.2 说明:

上一节我们已经学习了pick方法,omit方法会和pick有什么关联呢? omit方法刚好和pick方法相反,pick方法是返回的匹配成功的值,但是omit方法将是过滤掉匹配成功的值,*args可为多个属性名,数组,函数。(数组可和属性名同时使用)

2.13.3 代码示例:

示例一:根据多个属性名进行匹配

var result = _.omit({ one: '一', two: '二', three: '三' }, "one", "two");
console.log(result) //=> {three: "三"}

示例二:根据数组进行匹配

var result;
result = _.omit({ one: '一', two: '二', three: '三' }, ["three"]);
console.log(result) //=> {one: "一", two: "二"}

示例三:根据函数进行匹配

var result = _.omit({ one: '一', two: 2, three: 3 }, function (value, key, list) {
    return typeof value === "string";
});
console.log(result); //=> {two: 2, three: 3}

2.13.4 属性名,数组同时使用

var result = _.omit({ one: '一', two: '二', three: '三' }, ["two"], "three");
console.log(result) //=> {one: "一"}

2.13.5 obj为数组的情况

var result = _.omit([1, 2], "0");
console.log(result) //=> {1: 2}

2.12.6 obj为函数

var Child = function () { };
Child.prototype = {
    name: 'zhangsan',
    age: 18
}
var result = _.omit(new Child, "name");
console.log(result); //=> {age: 18}

2.12.7 参数名前边有系统方法的时候(参数名前边有系统方法则需要执行系统方法)

var result;
result = _.omit({ one: '一', two: 2, three: 3 }, Math.abs, "one", "two", "three");
console.log(result); //=> {one: "一"}

// Math.abs是最后一个参数的时候(不执行系统方法)
result = _.omit({ one: '一', two: 2, three: 3 }, "one", "three", Math.abs);
console.log(result); //=> {two: 2}

2.13.8 特殊情况

示例一:*args非属性名,数组,函数会返回什么呢?这里会和pick有什么区别?

var result = _.omit({ one: '一', two: '二', three: '三' }, true);
console.log(result) //=> {one: "一", two: "二", three: "三"}

示例二:pick方法参数为空

var result = _.omit();
console.log(result) //=> {}