简体   繁体   中英

Array comparation in javascripts

I would like to compare arrays nested in arrays.

For example, I have an array like this:

var myArrays = [[1,2],[1,2,3],[1,5,6,7,2.3,4],[1,2,3,2],[1,2,3],[1,2,3]];

How could I know the difference length of each array from loop step by step? I'm looking for an algorithm that will help solve this problem.

Thanks.

Use the map function. It accepts as the first parameter each item in your array and will return an array of what you will return from it . In your case you need to return the length of your every nested array.

 var myArrays = [[1,2],[1,2,3],[1,5,6,7,2.3,4],[1,2,3,2],[1,2,3],[1,2,3]]; var lengths = myArrays.map(arr => arr.length); console.log(lengths); 

For IE

 var myArrays = [[1,2],[1,2,3],[1,5,6,7,2.3,4],[1,2,3,2],[1,2,3],[1,2,3]]; var lengths = myArrays.map( function(arr) { return arr.length; }); console.log(lengths); 

'data' array is a multi-dimensional array which you can iterate through it like below and find for example size of each one.

      for(var i = 0; i < 10; i++) {
            for(var j = 0; j < 10; j++) {
                if(data[i][j].length > 0) {
                    do sth on data[i][j]....
                }
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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