Mangler.index()
Builds a lookup object using object properties or a custom key generator.
Mangler.index(array, generator[, delimiter])
Parameter | Type | Default | Description |
---|---|---|---|
array | Array Mangler |
An array of objects to build the index for. Passing a mangler object will build an index from its items. | |
generator | String Array.String Function |
If a string or array of strings is passed, the keys used to build the index will be read by Mangler.getPath() from each object. If a function is passed, it will be called for each item with parameters function(index, item) , and it must return either a string to be used as a key, or false to leave the item out of the index. |
|
delimiter | String | `' | '` |
Returns the generated lookup object.
This example will build an index on the id
property to easily find the object to change.
data = [
{ id: '001', name: 'Mr Smith' },
{ id: '002', name: 'John' },
{ id: '003', name: 'Jack' }
];
lookup = Mangler.index(data, 'id');
/*
lookup = {
'001': { id: '001', name: 'Mr Smith' },
'002': { id: '002', name: 'John' },
'003': { id: '003', name: 'Jack' }
}
*/
Now it is easy to change the name of a record by its id
:
lookup['002'].name = 'Bill';
Because of the preserved object references, the name was changed in the original data as well, and its structure is untouched.
This method is useful if you use the returned lookup object several times. In case you only do a single query, consider using Mangler.first() instead:
Mangler.first(data, { id: '002' }).name = 'Bill';
You can pass and array of strings to build a composite key:
data = [
{ part1: 'A', part2: '001', name: 'Mr Smith' },
{ part1: 'B', part2: '001', name: 'John' },
{ part1: 'B', part2: '002', name: 'Jack' }
];
lookup = Mangler.index(data, ['part1', 'part2']);
/*
lookup = {
'A|001': { part1: 'A', part2: '001', name: 'Mr Smith' },
'B|001': { part1: 'B', part2: '001', name: 'John' },
'B|002': { part1: 'B', part2: '002', name: 'Jack' }
}
*/
To change the default delimiter, pass a third parameter:
lookup = Mangler.index(data, ['part1', 'part2'], '-');
/*
lookup = {
'A-001': { part1: 'A', part2: '001', name: 'Mr Smith' },
'B-001': { part1: 'B', part2: '001', name: 'John' },
'B-002': { part1: 'B', part2: '002', name: 'Jack' }
}
*/
To generate the index keys yourself, or to filter the items that goes into the index, you can use a key generator function as the generator
parameter. The following example filters out cats and builds a unique ID for dogs:
data = [
{ type: 'dog', name: 'Fred' },
{ type: 'cat', name: 'Fluffy' },
{ type: 'dog', name: 'Oscar' }
];
dogs = Mangler.index(data, function(i, v) {
return v.type === 'dog' ? v.type.toUpperCase() + i : false;
});
/*
dogs = {
'DOG0': { type: 'dog', name: 'Fred' },
'DOG2': { type: 'dog', name: 'Oscar' }
}
*/
Mangler.js - JavaScript object processing library
Copyright (C) 2014-2016
Project: http://codebin.co.uk/projects/mangler-js/
GitHub: https://github.com/DarthJDG/Mangler.js
- Home
- About the documentation
- Getting started
- Searching and querying
- Transforming data
- Advanced