@stdlib/array-base-entries2views
v0.1.1
Published
Convert array entries to an array of composite views.
Readme
entries2views
Convert array entries to an array of composite views.
Installation
npm install @stdlib/array-base-entries2viewsUsage
var entries2views = require( '@stdlib/array-base-entries2views' );entries2views( arr, fields )
Converts array entries to an array of composite views.
var x = [ 1, 2, 3 ];
var fields = [ 'x', 'y' ];
var out = entries2views( x, fields );
// returns [ <Object>, <Object>, <Object> ]
var v0 = out[ 0 ].toJSON();
// returns { 'x': 0, 'y': 1 }
var v1 = out[ 1 ].toJSON();
// returns { 'x': 1, 'y': 2 }
var v2 = out[ 2 ].toJSON();
// returns { 'x': 2, 'y': 3 }
// Mutate the input array:
x[ 0 ] = 5;
v0 = out[ 0 ].toJSON();
// returns { 'x': 0, 'y': 5 }
// Set a view property:
out[ 1 ].y = 'beep';
v1 = out[ 1 ].toJSON();
// returns { 'x': 1, 'y': 'beep' }
var y = x.slice();
// returns [ 5, 'beep', 3 ]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 list of field names should be a two-element array where the first element corresponds to the field name of input array element index and the second element corresponds to the field name of the input array element value.
- For each element of the returned array, the index view field is read-only and cannot be mutated.
- Each view in the returned array shares the same memory as the corresponding element in the input array. Accordingly, mutation of either the input array or a view will mutate the other.
Examples
var discreteUniform = require( '@stdlib/random-array-discrete-uniform' );
var entries2views = require( '@stdlib/array-base-entries2views' );
var x = discreteUniform( 10, -100, 100 );
var fields = [ 'x', 'y' ];
var out = entries2views( 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.
