_.pluck(list, key)
pluck方法根据key对list数组中的每个对象进行检索,返回检索成功的属性值,否则返回undefined,返回一个数组
var result;
// 操作数组对象
result = _.pluck([{name: 'moe', age: 30}, {name: 'curly', age: 50}], 'name');
console.log(result); //=> ["moe", "curly"]
//操作arguments
function abc() {
result = _.pluck([{name: 'moe', age: 30}, {name: 'curly', age: 50}], 'name');
console.log(result); //=> ["1.0", "2.0", "3.0"]
}
abc({name: 'moe', age: 30}, {name: 'curly', age: 50});
var result = _.pluck([{ name: 'moe', age: 30 }, { name: 'curly', age: 50 }], 'sex');
console.log(result); //=> [undefined, undefined]
var result;
// list为字符串
result = _.pluck('ab', 'sex');
console.log(result); //=> [undefined, undefined]
// list为对象
result = _.pluck({ x: 1, y: 2 }, 'sex');
console.log(result); //=> [undefined, undefined]
var result;
result = _.pluck([{ '[object Object]': 1 }, { x: 2 }], {});
console.log(result); //=> [1, undefined]
// list为null、true、undefined等等则返回一个空数组
result = _.pluck(null, 'sex');
console.log(result); //=> []