npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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

17

Readme

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' } }]