Array Shape

Determine (nested) array dimensions.

Usage

var arrayShape = require( '@stdlib/array/shape' );

arrayShape( arr )

Returns array dimensions.

var arr = [
    [ 1, 2, 3 ],
    [ 4, 5, 6 ],
    [ 7, 8, 9 ]
];

var shape = arrayShape( arr );
// returns [ 3, 3 ]

The function ignores inconsistent dimensions.

var arr = [
    [ 1, 2, 3 ],
    [ 4, 5, 6 ],
    [ 7, 8 ]
];

var shape = arrayShape( arr );
// returns [ 3 ]

Examples

var arrayShape = require( '@stdlib/array/shape' );

var arr = [ 1, 2, 3 ];
var shape = arrayShape( arr );
// returns [ 3 ]

arr = [
    [ 1 ],
    [ 2 ],
    [ 3 ]
];
shape = arrayShape( arr );
// returns [ 3, 1 ]

arr = [
    [],
    [],
    []
];
shape = arrayShape( arr );
// returns [ 3, 0 ]

arr = [
    [ 1, 2, 3 ]
];
shape = arrayShape( arr );
// returns [ 1, 3 ]

arr = [
    [ [ 1 ] ],
    [ [ 2 ] ],
    [ [ 3 ] ]
];
shape = arrayShape( arr );
// returns [ 3, 1, 1 ]

arr = [ [ [ [ 1, 2, 3 ] ] ] ];
shape = arrayShape( arr );
// returns [ 1, 1, 1, 3 ]

arr = [
    [ 1, 2 ],
    [ 3, 4 ]
];
shape = arrayShape( arr );
// returns [ 2, 2 ]

arr = [
    [ 1, 2, 3 ],
    [ 4, 5, 6 ],
    [ 7, 8, 9 ]
];
shape = arrayShape( arr );
// returns [ 3, 3 ]

arr = [
    [ 1, 2, 3 ],
    null,
    [ 7, 8, 9 ]
];
shape = arrayShape( arr );
// returns [ 3 ]

arr = [
    [ 1, 2, 3 ],
    [ [ 4, 5, 6 ] ],
    [ [ 7, 8, 9 ] ]
];
shape = arrayShape( arr );
// returns [ 3 ]

arr = [
    [ [ 1, 2, 3 ] ],
    [ 4, 5, 6 ],
    [ [ 7, 8, 9 ] ]
];
shape = arrayShape( arr );
// returns [ 3 ]

arr = [
    [ [ 1, 2, 3 ] ],
    [ [ 4, 5, 6 ] ],
    [ 7, 8, 9 ]
];
shape = arrayShape( arr );
// returns [ 3 ]

arr = [
    [ [ [ 1, 2, 3 ] ] ],
    [ [ 4, 5, 6 ] ],
    [ [ [ 7, 8, 9 ] ] ]
];
shape = arrayShape( arr );
// returns [ 3, 1 ]
Did you find this page helpful?