Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

rekey

Copy and rename specified keys for every element in a provided array.

Usage

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 [...]