transform-array-list
TypeScript icon, indicating that this package has built-in type declarations

0.6.3 • Public • Published

Transforming an array of objects with duplicate objects into single objects with an array of duplicate data as elements of objects

npm npm bundle size (minified) licence (MIT)

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 specified

    const { objToArray } = require('transform-array-list');
  • options

    1. 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]
    1. 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]
    1. 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']
    1. 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]]
    1. 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

    1. 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 }]
    1. second option
    const result = arrayToObject(
      [
        ['Male', 'Isaac'],
        ['Female', 'annet'],
      ],
      0
    );
    // type is specified as 0
    //output -> [{ 0: ['Male', 'Isaac'] }, { 1: ['Female', 'annet'] }]
    1. 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' },]
    1. 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' } },]
    1. 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' } }]

Package Sidebar

Install

npm i transform-array-list

Weekly Downloads

2

Version

0.6.3

License

MIT

Unpacked Size

16.7 kB

Total Files

5

Last publish

Collaborators

  • isaacssemugenyi