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

后端 未结 9 2146
迷失自我
迷失自我 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:14

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

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

提交回复
热议问题