transform-array-list
v0.6.3
Published
Transforming an array of objects with duplicate objects into single objects with an array of duplicate data as elements of objects
Downloads
23
Maintainers
Readme
Transforming an array of objects with duplicate objects into single objects with an array of duplicate data as elements of objects
Usage
transformList
const TransformArrayList = require('transform-array-list');
// or
const { transformList, includesAll } = require('transform-array-list');
// List to Transform
const itemList = [
{ id: 1, name: 'Isaac', age: 10, address: 'Kampala' },
{ id: 2, name: 'Annet', age: 20, address: 'Wakiso' },
{ id: 1, name: 'Phesto', age: 13, address: 'Mutundwe' },
];
// List of fields Array to return in the nested arrays
const fieldsArray = ['name', 'address'];
// Identifier -> One used as key of reference
const identifier = 'id';
// optional lookup list key (default is lookup)
const lookup = 'details';
// const TransformArrayList = require('transform-array-list');
const result = TransformArrayList.transformList(
itemList,
fieldsArray,
identifier,
lookup
);
// if const { transformList } = require('transform-array-list');
const result = transformList(itemList, fieldsArray, identifier, lookup);
console.log(result);
//Output
[
{
id: 1,
age: 10,
details: [
{
name: 'Isaac',
address: 'Kampala',
},
{
name: 'Phesto',
address: 'Mutundwe',
},
],
},
{
id: 2,
age: 20,
details: [
{
name: 'Annet',
address: 'Wakiso',
},
],
},
];
includesAll
require includes from transformArrayList
first argument
const arrToCheckWith = ['isaac', 'tom', 'jack'];
second argument
const arrToCompare = ['tom', 'isaac'];
call the function and store passing these params and store the result in any variable
const result = includesAll(arrToCheckWith, arrToCompare); console.log(result); //output true;
objToArray
requires an object with properties and a type (number)
first argument value will be an object
second argument is a number 0, 1, 2, 3 and is optional, returning
object values
when not specifiedconst { objToArray } = require('transform-array-list');
options
- default option
const result = objToArray({ id: 1, name: 'Isaac', age: 30 }); // type is missing, so 0 will be used as the type and object values will be returned //output -> [1, 'Isaac', 30]
- second option
const result = objToArray({ id: 1, name: 'Isaac', age: 30 }, 0); // type is specified as 0, object values will be returned //output -> [1, 'Isaac', 30]
- third option
const result = objToArray({ id: 1, name: 'Isaac', age: 30 }, 1); // type is specified as 1, object keys will be returned //output -> ['id', 'name', 'age']
- forth option
const result = objToArray({ id: 1, name: 'Isaac', age: 30 }, 2); // type is 2, object entries returned as nested arrays // output -> [['id', 4], ['name', 'Isaac'], ['age', 25]]
- firth option
const result = objToArray({ id: 1, name: 'Isaac', age: 30 }, 3); // type is 3, object entries returned flattened // output -> ['id', 4, 'name', 'Isaac', 'age', 25]
arrayToObject
const { arrayToObject } = require('transform-array-list');
options
- default option
const result = arrayToObject([1, 2, 3]); // type is missing, so 0 will be used as the type and object values will be returned //output -> [{ 0: 1 }, { 1: 2 }, { 2: 3 }]
- second option
const result = arrayToObject( [ ['Male', 'Isaac'], ['Female', 'annet'], ], 0 ); // type is specified as 0 //output -> [{ 0: ['Male', 'Isaac'] }, { 1: ['Female', 'annet'] }]
- third option
const result = arrayToObject(['one', 'two', 'three'], 1); // type is specified as 1, array of objects returned with key 'item' //output -> [{ item: 'one' },{ item: 'two' },{ item: 'three' },]
- forth option
const result = arrayToObject( [ 'address', { default: 'Kampala', home: 'Mpigi', work: 'Wakiso' }, 'bio', { firstName: 'Isaac', lastName: 'Johns' }, ], 2 ); // type is 2, current element is used as key and proceeding element is set as the value in the returned array of object // output -> [{ address: { default: 'Kampala', home: 'Mpigi', work: 'Wakiso' } },{ bio: { firstName: 'Isaac', lastName: 'Johns' } },]
- firth option
const result = arrayToObject( [ [5, { name: 'isaac' }], [7, { name: 'annet' }], ], 3 ); // type is 3, main array elements (arrays as well), each nested array, its first element is set as key and second element as value // output -> [{ 5: { name: 'isaac' } }, { 7: { name: 'annet' } }]