@stdlib/array-base-nested2views
v0.1.1
Published
Convert nested arrays to composite views.
Readme
nested2views
Convert nested arrays to composite views.
Installation
npm install @stdlib/array-base-nested2viewsUsage
var nested2views = require( '@stdlib/array-base-nested2views' );nested2views( arr, fields )
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.
Examples
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 [...]Notice
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
Community
License
See LICENSE.
Copyright
Copyright © 2016-2026. The Stdlib Authors.
