underscore.js - 确定数组数组中的所有值是否匹配 [英] underscore.js - Determine if all values in an array of arrays match

查看:111
本文介绍了underscore.js - 确定数组数组中的所有值是否匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组数组,看起来像这样:

I have an array of arrays, which looks something like this:

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

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

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.

任何人有想法吗?

推荐答案

试试这个人(与订单无关):

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

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

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).

这篇关于underscore.js - 确定数组数组中的所有值是否匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆