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

后端 未结 9 2129
迷失自我
迷失自我 2020-12-20 11:30

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

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


        
9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-20 12:08

    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:

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

提交回复
热议问题