Convert between objects and arrays in many different ways. Wololoooo!
With npm:
$ npm install obrray
With Yarn:
$ yarn add obrray
Type: Array
Array to be converted.
const obrray = require('obrray');
obrray.toObject([]);
//=> {}
obrray.toObject([123]);
//=> { 0: 123 }
obrray.toObject([11, 22, 33]);
//=> { 0: 11, 1: 22, 2: 33 }
obrray.toObject([[11, 22], [33, 44], [55, 66]]);
//=> { 0: [11, 22], 1: [33, 44], 2: [55, 66] }
Type: Object
Type: boolean
Uses each array item both as key and value of the target object.
obrray.toObject([11, 22, 33], { sameKeyAndValue: true });
//=> { 11: 11, 22: 22, 33: 33 }
Type: boolean|object
Converts from a bidimensional array with key-value pairs.
// Standard key-value pairs array
obrray.toObject(
[
['name', 'Ford Prefect'],
['planet', 'Betelgeuse Seven'],
['nickname', 'lx']
], { keyAndValuePairs: true });
//=> {
// name: 'Ford Prefect',
// planet: 'Betelgeuse Seven',
// nickname: 'lx'
// }
// Array with custom indexes for key and value
obrray.toObject(
[
[147, false, 'Ford Prefect', [], 'name'],
[215, true, 'Betelgeuse Seven', [111], 'planet'],
[12, false, 'lx', [27, 1], 'nickname']
], { keyAndValuePairs: { keyIndex: 4, valueIndex: 2 } });
//=> {
// name: 'Ford Prefect',
// planet: 'Betelgeuse Seven',
// nickname: 'lx'
// }
Type: boolean
Converts from a bidimensional array with inverted key-value pairs.
// Inverted key-value pairs array
obrray.toObject(
[
['Ford Prefect', 'name'],
['Betelgeuse Seven', 'planet'],
['lx', 'nickname']
], { invertedKeyAndValuePairs: true });
//=> {
// name: 'Ford Prefect',
// planet: 'Betelgeuse Seven',
// nickname: 'lx'
// }
Type: boolean|object
Converts from an array of objects with key and value properties.
// Key-value properties with default names
obrray.toObject(
[
{ key: 'name', value: 'Ford Prefect' },
{ key: 'planet', value: 'Betelgeuse Seven' },
{ key: 'nickname', value: 'lx' }
], { keyAndValueObjects: true });
//=> {
// name: 'Ford Prefect',
// planet: 'Betelgeuse Seven',
// nickname: 'lx'
// }
// Key-value properties with custom names
obrray.toObject(
[
{ k: 'name', v: 'Ford Prefect' },
{ k: 'planet', v: 'Betelgeuse Seven' },
{ k: 'nickname', v: 'lx' }
], { keyAndValueObjects: { keyProperty: 'k', valueProperty: 'v' } });
//=> {
// name: 'Ford Prefect',
// planet: 'Betelgeuse Seven',
// nickname: 'lx'
// }
Type: function
Uses a callback function to map each array item in a custom way.
// Default approach, by returning the new value from the callback
obrray.toObject(
[
['name', 147, 27, 42, false, 15, 'Ford Prefect', false],
['planet', 'Betelgeuse Seven', [111]],
['nickname', 12, false, 'lx', [27, 1], 147, 98]
], {
mapper: (item) => {
for (let i = 1; i < item.length; i++) {
if (typeof item[i] !== 'string') continue;
return { key: item[0], value: item[i] };
}
}
});
//=> {
// name: 'Ford Prefect',
// planet: 'Betelgeuse Seven',
// nickname: 'lx'
// }
// Changing the target object from within the callback
obrray.toObject(
[
['name', 147, 27, 42, false, 15, 'Ford Prefect', false],
['planet', 'Betelgeuse Seven', [111]],
['nickname', 12, false, 'lx', [27, 1], 147, 98]
], {
mapper: (item, targetObj) => {
for (let i = 1; i < item.length; i++) {
if (typeof item[i] !== 'string') continue;
targetObj[item[0]] = item[i];
break;
}
}
});
//=> {
// name: 'Ford Prefect',
// planet: 'Betelgeuse Seven',
// nickname: 'lx'
// }
Type: Object
Object to be converted.
obrray.toArray({});
//=> []
obrray.toArray({ whatever: 123 });
//=> [123]
obrray.toArray({ name: 'Vito', surname: 'Corleone' });
//=> ['Vito', 'Corleone']
obrray.toArray({ 2: 'Corleone', 1: 'Andolini', 0: 'Vito' });
//=> ['Vito', 'Andolini', 'Corleone']
obrray.toArray({ foo: 'bar', 2: 'Corleone', 1: 'Andolini', 0: 'Vito', bar: 'foo' });
//=> ['Vito', 'Andolini', 'Corleone', 'bar', 'foo']
Type: boolean
Uses property keys instead of values for each item of the mapped array
obrray.toArray({ name: 'Vito', surname: 'Corleone' }, { useKeys: true });
//=> ['name', 'surname']
Type: boolean
Converts to an array of key and value pairs
obrray.toArray(
{
name: 'Vito',
middleName: 'Andolini',
surname: 'Corleone'
}, { toKeyAndValuePairs: true });
//=> [['name', 'Vito'], ['middleName', 'Andolini'], ['surname', 'Corleone']]
Type: boolean
Converts to an array of inverted key and value pairs
obrray.toArray(
{
name: 'Vito',
middleName: 'Andolini',
surname: 'Corleone'
}, { toInvertedKeyAndValuePairs: true });
//=> [['Vito', 'name'], ['Andolini', 'middleName'], ['Corleone', 'surname']]
Type: boolean|object
Converts to an array of key and value objects
// Using default key-value property names
obrray.toArray(
{
name: 'Vito',
middleName: 'Andolini',
surname: 'Corleone'
}, { toKeyAndValueObjects: true });
//=> [{ key: 'name', value: 'Vito' }, { key: 'middleName', value: 'Andolini' }, { key: 'surname', value: 'Corleone' }]
// Using custom key-value property names
obrray.toArray(
{
name: 'Vito',
middleName: 'Andolini',
surname: 'Corleone'
}, { toKeyAndValueObjects: { keyProperty: 'k', valueProperty: 'v' } });
//=> [{ k: 'name', v: 'Vito' }, { k: 'middleName', v: 'Andolini' }, { k: 'surname', v: 'Corleone' }]
Type: function
Sorts the output array
obrray.toArray(
{
name: 'Vito',
middleName: 'Andolini',
surname: 'Corleone'
}, { sorter: (a, b) => a.value.localeCompare(b.value) });
//=> ['Andolini', 'Corleone', 'Vito']
// Using a sorter along with other option
obrray.toArray(
{
name: 'Vito',
middleName: 'Andolini',
surname: 'Corleone'
}, { sorter: (a, b) => a.value.localeCompare(b.value), toKeyAndValuePairs: true });
//=> [['middleName', 'Andolini'], ['surname', 'Corleone'], ['name', 'Vito']]
Type: function
Uses a callback function to map each object property in a custom way.
// Default approach, by returning the new value from the callback
obrray.toArray(
{
name: 'Vito',
middleName: 'Andolini',
surname: 'Corleone'
}, { mapper: (item) => `The ${item.key} is "${item.value}"` });
//=> ['The name is "Vito"', 'The middleName is "Andolini"', 'The surname is "Corleone"']
// Changing the target array from within the callback
obrray.toArray(
{
name: 'Vito',
middleName: 'Andolini',
surname: 'Corleone'
},
{
mapper: (item, targetArr) => {
targetArr.push(`The ${item.key} is "${item.value}"`);
targetArr.push('...an additional item...');
}
});
//=> [
// 'The name is "Vito"',
// '...an additional item...',
// 'The middleName is "Andolini"',
// '...an additional item...',
// 'The surname is "Corleone"',
// '...an additional item...'
// ]
Alcides Queiroz Aguiar
- Website: www.alcidesqueiroz.com
- Medium: @alcidesqueiroz
- Twitter: alcidesqueiroz
- Behance alcidesqueiroz
- Stack Overflow: http://is.gd/aqanso
- E-mail: alcidesqueiroz <at> gmail
This code is free to use under the terms of the MIT License.