我有一个Javascript数组,我想根据每个元素上调用的函数是返回true还是分成两个false.从本质上讲,这是一个array.filter,但我也想了解过滤掉的元素.
目前,我的计划是array.forEach在每个元素上使用和调用谓词函数.根据这是真还是假,我将当前元素推送到两个新数组中的一个.是否有更优雅或更好的方法来做到这一点?例如array.filter,在将元素返回之前将元素推送到另一个数组的位置false?
bra*_*aza 46
使用ES6,您可以使用spread语法和reduce:
function partition(array, isValid) {
return array.reduce(([pass, fail], elem) => {
return isValid(elem) ? [[...pass, elem], fail] : [pass, [...fail, elem]];
}, [[], []]);
}
const [pass, fail] = partition(myArray, (e) => e > 5);
Run Code Online (Sandbox Code Playgroud)
或者在一行上:
const [pass, fail] = a.reduce(([p, f], e) => (e > 5 ? [[...p, e], f] : [p, [...f, e]]), [[], []]);
Run Code Online (Sandbox Code Playgroud)
Zol*_*han 25
你可以使用lodash.partition
var users = [
{ 'user': 'barney', 'age': 36, 'active': false },
{ 'user': 'fred', 'age': 40, 'active': true },
{ 'user': 'pebbles', 'age': 1, 'active': false }
];
_.partition(users, function(o) { return o.active; });
// ? objects for [['fred'], ['barney', 'pebbles']]
// The `_.matches` iteratee shorthand.
_.partition(users, { 'age': 1, 'active': false });
// ? objects for [['pebbles'], ['barney', 'fred']]
// The `_.matchesProperty` iteratee shorthand.
_.partition(users, ['active', false]);
// ? objects for [['barney', 'pebbles'], ['fred']]
// The `_.property` iteratee shorthand.
_.partition(users, 'active');
// ? objects for [['fred'], ['barney', 'pebbles']]
Run Code Online (Sandbox Code Playgroud)
R.partition(R.contains('s'), ['sss', 'ttt', 'foo', 'bars']);
// => [ [ 'sss', 'bars' ], [ 'ttt', 'foo' ] ]
R.partition(R.contains('s'), { a: 'sss', b: 'ttt', foo: 'bars' });
// => [ { a: 'sss', foo: 'bars' }, { b: 'ttt' } ]
Run Code Online (Sandbox Code Playgroud)
小智 21
我想到了这个小家伙。它用于您所描述的所有内容,但在我看来它看起来干净简洁。
//Partition function
function partition(array, filter) {
let pass = [], fail = [];
array.forEach((e, idx, arr) => (filter(e, idx, arr) ? pass : fail).push(e));
return [pass, fail];
}
//Run it with some dummy data and filter
const [lessThan5, greaterThanEqual5] = partition([0,1,4,3,5,7,9,2,4,6,8,9,0,1,2,4,6], e => e < 5);
//Output
console.log(lessThan5);
console.log(greaterThanEqual5);Run Code Online (Sandbox Code Playgroud)
Bra*_*dan 14
这听起来与Ruby的Enumerable#partition方法非常相似.
如果函数不能有副作用(即,它不能改变原始数组),那么对于对数组进行分区而不是迭代每个元素并将元素推送到两个数组中的一个时,没有更有效的方法.
话虽这么说,创建一个方法Array来执行这个功能可能更"优雅" .在这个例子中,filter函数在原始数组的上下文中执行(即,this将是原始数组),并且它接收元素和元素的索引作为参数(类似于jQuery的each方法):
Array.prototype.partition = function (f){
var matched = [],
unmatched = [],
i = 0,
j = this.length;
for (; i < j; i++){
(f.call(this, this[i], i) ? matched : unmatched).push(this[i]);
}
return [matched, unmatched];
};
console.log([1, 2, 3, 4, 5].partition(function (n, i){
return n % 2 == 0;
}));
//=> [ [ 2, 4 ], [ 1, 3, 5 ] ]
Run Code Online (Sandbox Code Playgroud)
par*_*omi 12
这里的很多答案都用于Array.prototype.reduce构建可变累加器,并正确地指出,对于大型数组,这比使用扩展运算符在每次迭代时复制新数组更有效。缺点是它不如使用短 lambda 语法的“纯”表达式漂亮。
但解决这个问题的方法是使用逗号运算符。在类 C 语言中,逗号是一个始终返回右侧操作数的运算符。您可以使用它来创建一个调用 void 函数并返回值的表达式。
function partition(array, predicate) {
return array.reduce((acc, item) => predicate(item)
? (acc[0].push(item), acc)
: (acc[1].push(item), acc), [[], []]);
}
Run Code Online (Sandbox Code Playgroud)
如果您利用布尔表达式隐式转换为数字 0 和 1 的事实,您可以使其更加简洁,尽管我认为它不那么可读:
function partition(array, predicate) {
return array.reduce((acc, item) => (acc[+!predicate(item)].push(item), acc), [[], []]);
}
Run Code Online (Sandbox Code Playgroud)
用法:
const [trues, falses] = partition(['aardvark', 'cat', 'apple'], i => i.startsWith('a'));
console.log(trues); // ['aardvark', 'apple']
console.log(falses); // ['cat']
Run Code Online (Sandbox Code Playgroud)
您可以使用reduce:
function partition(array, callback){
return array.reduce(function(result, element, i) {
callback(element, i, array)
? result[0].push(element)
: result[1].push(element);
return result;
}, [[],[]]
);
};
Run Code Online (Sandbox Code Playgroud)
在过滤函数中,您可以将虚假项目推送到函数外的另一个变量:
var bad = [], good = [1,2,3,4,5];
good = good.filter(function (value) { if (value === false) { bad.push(value) } else { return true});
Run Code Online (Sandbox Code Playgroud)
当然value === false需要真正的比较;)
但它几乎完成了同样的操作forEach.我认为你应该使用forEach更好的代码可读性.
那这个呢?
[1,4,3,5,3,2].reduce( (s, x) => { s[ x > 3 ].push(x); return s;} , {true: [], false:[]} )
Run Code Online (Sandbox Code Playgroud)
可能这比扩展运算符更有效
或者更短,但更丑
[1,4,3,5,3,2].reduce( (s, x) => s[ x > 3 ].push(x)?s:s , {true: [], false:[]} )
Run Code Online (Sandbox Code Playgroud)
尝试这个:
function filter(a, fun) {
var ret = { good: [], bad: [] };
for (var i = 0; i < a.length; i++)
if (fun(a[i])
ret.good.push(a[i]);
else
ret.bad.push(a[i]);
return ret;
}
Run Code Online (Sandbox Code Playgroud)
轻松读一读。
const partition = (arr, condition) => {
const trues = arr.filter(el => condition(el));
const falses = arr.filter(el => !condition(el));
return [trues, falses];
};
// sample usage
const nums = [1,2,3,4,5,6,7]
const [evens, odds] = partition(nums, (el) => el%2 == 0)
Run Code Online (Sandbox Code Playgroud)
我最终这样做是因为它很容易理解:
const partition = (array, isValid) => {
const pass = []
const fail = []
array.forEach(element => {
if (isValid(element)) {
pass.push(element)
} else {
fail.push(element)
}
})
return [pass, fail]
}
// usage
const [pass, fail] = partition([1, 2, 3, 4, 5], (element) => element > 3)
Run Code Online (Sandbox Code Playgroud)
相同的方法包括打字稿的类型:
const partition = <T>(array: T[], isValid: (element: T) => boolean): [T[], T[]] => {
const pass: T[] = []
const fail: T[] = []
array.forEach(element => {
if (isValid(element)) {
pass.push(element)
} else {
fail.push(element)
}
})
return [pass, fail]
}
// usage
const [pass, fail] = partition([1, 2, 3, 4, 5], (element: number) => element > 3)
Run Code Online (Sandbox Code Playgroud)