@stdlib/array-base-rekey
v0.1.1
Published
Copy and rename specified keys for every element in a provided array.
Readme
rekey
Copy and rename specified keys for every element in a provided array.
Installation
npm install @stdlib/array-base-rekeyUsage
var rekey = require( '@stdlib/array-base-rekey' );rekey( arr, mapping )
Copies and renames specified keys for every element in a provided array.
var x = [
{
'x': 1,
'y': 2
},
{
'x': 3,
'y': 4
}
];
var mapping = {
'x': 'a',
'y': 'b'
};
var out = rekey( x, mapping );
// returns [ { 'a': 1, 'b': 2 }, { 'a': 3, 'b': 4 } ]The function has the following parameters:
- arr: input array.
- mapping: object mapping existing keys to new key names.
Notes
- The function only copies and renames those keys which are present in a provided mapping object. Any keys which are not present in the provided mapping object, but are present in the original objects, are omitted during object creation.
- The function assumes that each object has the keys specified in a provided mapping object.
- The function performs shallow copies of key values.
Examples
var discreteUniform = require( '@stdlib/random-base-discrete-uniform' );
var filledBy = require( '@stdlib/array-base-filled-by' );
var rekey = require( '@stdlib/array-base-rekey' );
function clbk( idx ) {
return {
'x': idx,
'y': discreteUniform( 0, 10 )
};
}
var x = filledBy( 10, clbk );
var mapping = {
'x': 'a',
'y': 'b'
};
var out = rekey( x, mapping );
// 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.
