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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10110510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:52:46  来源:igfitidea点击:

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

javascriptunderscore.js

提问by Kevin Whitaker

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 _.allmethod in Underscoreto 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.

我想我可以使用Underscore 中_.all方法来确定所有数组是否都匹配 100%(即它们的所有值都匹配),但我不确定如何编写所需的迭代器来运行检查。

Anyone have an idea?

有人有想法吗?

采纳答案by Dan Tao

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 orderas one another (so for your example input the function should return false).

这是假设您希望所有数组以彼此相同的顺序包含所有相同的元素(因此对于您的示例输入,函数应该返回false)。

回答by peterfoldi

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.

如果数组的长度相同,并且交集的长度等于数组的长度,则它们都包含相同的值。

回答by paulmelnikow

My preference:

我的偏好:

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

And if order matters:

如果订单很重要:

_(first).isEqual(second)

回答by Craig Myles

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 trueif they are the same and falseif they are not.

true如果它们相同,false如果它们不同,这将返回。

回答by Ron

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

回答by jazmit

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]

回答by klodoma

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);
}

回答by chbrown

If you don't need to know whichelements 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

回答by cotko

I can't comment on Dan Tao's answer, small change to skip checking of first array against itself (unnecessary _.difference call)

我无法评论 Dan Tao 的回答,小改动跳过对自身的第一个数组的检查(不必要的 _.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;
    });
}