简体   繁体   English

underscore.js - 确定数组数组中的所有值是否匹配

[英]underscore.js - Determine if all values in an array of arrays match

I have an array of arrays, which looks something like this: 我有一个数组数组,看起来像这样:

[["Some string", "Some other string"],["Some third string", "some fourth string"]]

I think I can use the _.all method in Underscore to determine if all of the arrays match 100% (that is all of their values match), but I'm not sure how to write the required iterator to run the check. 我想我可以使用_.all的方法下划线 ,以确定是否所有的阵列匹配100%(即所有的值相匹配的),但我不知道怎么写所需的迭代器运行检查。

Anyone have an idea? 有人有想法吗?

Why not intersection? 为什么不交? (if you really want to use some Underscore functions for this) http://underscorejs.org/#intersection (如果你真的想使用一些Underscore函数) http://underscorejs.org/#intersection

If the arrays are of the same length, and the length of the intersection equals to the length of the arrays, then they all contain the same values. 如果数组长度相同,并且交集的长度等于数组的长度,则它们都包含相同的值。

My preference: 我的偏好:

_.isEqual(_.sortBy(first), _.sortBy(second))

And if order matters: 如果订单很重要:

_(first).isEqual(second)

Try this guy (order-independent): 试试这个人(与订单无关):

function allArraysAlike(arrays) {
  return _.all(arrays, function(array) {
    return array.length == arrays[0].length && _.difference(array, arrays[0]).length == 0;
  });
}

This is assuming you want all of the arrays to contain all the same elements in the same order as one another (so for your example input the function should return false ). 这假设您希望所有数组以 相同的顺序 包含所有相同的元素(因此对于您的示例输入,该函数应返回false )。

Use underscore to determine the difference between the two, and check the length of the array. 使用下划线确定两者之间的差异,并检查数组的长度。 An easy way to do this would be: 一个简单的方法是:

_.isEmpty(_.difference(array1, array2)) && _.isEmpty(_.difference(array2, array1))

This will return true if they are the same and false if they are not. 如果它们相同则返回true否则返回false

_.isEmpty(_.xor(array1, array2))

If you want to check that the elements are the same and in the same order, I would go with: 如果你想检查元素是否相同且顺序相同,我会选择:

arrayEq = function(a, b) {
  return _.all(_.zip(a, b), function(x) {
    return x[0] === x[1];
  });
};

Even more neatly in coffeescript: 在coffeescript中更加整洁:

arrayEq = (a,b) ->
    _.all _.zip(a,b), (x) -> x[0]==x[1]

My implementation with http://underscorejs.org/ 我在http://underscorejs.org/的实施

/**
 * Returns true if the arrays are equal
 *
 * @param {Array} array1
 * @param {Array} array2
 * @returns {boolean}
 */
equal: function ( array1, array2 )
{
    return ( array1.length === array2.length)
        && (array1.length === _.intersection( array1, array2 ).length);
}

If you don't need to know which elements are unequal, use transitivity: 如果您不需要知道哪些元素不相等,请使用传递性:

function allEqual(list) {
  return _.all(list.slice(1), _.partial(_.isEqual, list[0]));
}
allEqual([2, 2, 2, 2]) //=> true
allEqual([2, 2, 3, 2]) //=> false
allEqual([false])      //=> true
allEqual([])           //=> true

I can't comment on Dan Tao's answer, small change to skip checking of first array against itself (unnecessary _.difference call) 我不能评论丹涛的答案,小改为跳过检查第一个阵列对自己(不必要的_.difference调用)

function allArraysAlike(arrays) {
    return _.all(arrays, function(array) {
        if(array === arrays[0]) return true;
        return array.length == arrays[0].length && _.difference(array, arrays[0]).length == 0;
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM