Convert nested arrays to composite views.
var nested2views = require( '@stdlib/array/base/nested2views' );Converts each nested array to a composite view.
var x = [ [ 1, 2 ], [ 3, 4 ] ];
var fields = [ 'x', 'y' ];
var out = nested2views( x, fields );
// returns [ <Object>, <Object> ]
var v0 = out[ 0 ].toJSON();
// returns { 'x': 1, 'y': 2 }
var v1 = out[ 1 ].toJSON();
// returns { 'x': 3, 'y': 4 }
// Mutate the first nested array:
x[ 0 ][ 0 ] = 5;
v0 = out[ 0 ].toJSON();
// returns { 'x': 5, 'y': 2 }
// Set a view property:
out[ 1 ].y = 'beep';
v1 = out[ 1 ].toJSON();
// returns { 'x': 3, 'y': 'beep' }
var y = x.slice();
// returns [ [ 5, 2 ], [ 3, 'beep' ] ]The function supports the following parameters:
- arr: input array.
- fields: list of field names.
Each element in the returned array is a class instance having prototype properties corresponding to the list of field names. As demonstrated in the example above, to convert an element to a regular object, invoke an element's toJSON method. Note, however, that the object returned by an element's toJSON method no longer shares the same memory as the provided input array.
- The function assumes that all nested arrays have the same length.
- The number of provided array labels should equal the length of each nested array.
- Each view in the returned array shares the same memory as the corresponding element in the input array. Accordingly, mutation of either a nested array or a view will mutate the other.
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
var filled2dBy = require( '@stdlib/array/base/filled2d-by' );
var nested2views = require( '@stdlib/array/base/nested2views' );
var x = filled2dBy( [ 10, 2 ], discreteUniform( -100, 100 ) );
var fields = [ 'x', 'y' ];
var out = nested2views( x, fields );
// returns [...]