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

@abydin/convert-array-to-object

v2.4.0

Published

This s a lightweight and versatile npm package designed to streamline the transformation of arrays into structured JavaScript objects. Whether you're dealing with data manipulation, API responses, or form submissions, this utility simplifies the process o

Downloads

19

Readme

convert-array-to-object

License: MIT

Effortlessly convert arrays into structured JavaScript objects with the convert-array-to-object npm package. This lightweight utility simplifies the process of transforming arrays into key-value paired objects, enhancing your data manipulation tasks.

Installation

install with Npm

npm install @abydin/convert-array-to-object

install with Yarn

yarn add @abydin/convert-array-to-object

Usage

Here are some examples of how to use the convertArrToObj function:

Usage

const convertArrToObj = require("@abydin/convert-array-to-object");

// Example usage:
const inputArray = [
	{ id: 1, name: "John" },
	{ id: 2, name: "Jane" },
	{ id: 3, name: "Doe" },
];

// Without reArr option
const outputObject = convertArrToObj({
	arr: inputArray,
	callBack: (item) => item.name,
	key: "id",
});

console.log(outputObject);
// Output: { '1': 'John', '2': 'Jane', '3': 'Doe' }

// With reArr option
const outputArray = convertArrToObj({
	arr: inputArray,
	callBack: (item) => item.name,
	key: "id",
	reArr: true,
});

console.log(outputArray);
// Output: [ 'John', 'Jane', 'Doe' ]

API

convertArrToObj(options)

Parameters

  • options (Object):
    • arr (Array): The input array of objects.
    • callBack (Function, optional): A callback function to transform each item in the array.
    • key (String): The key to use as the property in the resulting object or array.
    • reArr (Boolean, optional): If set to true, the function returns an array of values instead of an object.

Returns

  • If reArr is false (default): Returns an object where keys are taken from the specified key and values are the result of the optional callBack function.
  • If reArr is true: Returns an array of values derived from the specified key and optional callBack function.

Examples

Basic Usage

const inputArray = [
	{ id: 1, name: "John" },
	{ id: 2, name: "Jane" },
	{ id: 3, name: "Doe" },
];

const outputObject = convertArrToObj({
	arr: inputArray,
	key: "id",
});

console.log(outputObject);
// Output: { '1': { id: 1, name: 'John' }, '2': { id: 2, name: 'Jane' }, '3': { id: 3, name: 'Doe' } }

Transforming Values with Callback

You can use the callBack option to transform values during the conversion process. The callBack function takes each item in the array as its argument and should return the transformed value.

Example

const inputArray = [
	{ id: 1, name: "John" },
	{ id: 2, name: "Jane" },
	{ id: 3, name: "Doe" },
];

const outputObject = convertArrToObj({
	arr: inputArray,
	key: "id",
	callBack: (item) => item.name.toUpperCase(),
});

console.log(outputObject);
// Output: { '1': 'JOHN', '2': 'JANE', '3': 'DOE' }

Using reArr Option

The reArr option allows you to control the output format. When set to true, the function returns an array of values instead of an object.

Example

Consider an array of user objects:

const users = [
	{ id: 1, name: "John", age: 25 },
	{ id: 2, name: "Jane", age: 30 },
	{ id: 3, name: "Doe", age: 22 },
];
const userNamesArray = convertArrToObj({
	arr: users,
	key: "id",
	callBack: (user) => user.name,
	reArr: true,
});

console.log(userNamesArray);
// Output: [ 'John', 'Jane', 'Doe' ]

Keywords

  • convert-arr-to-obj
  • array conversion
  • object transformation
  • data manipulation
  • JavaScript utility
  • array to object mapping
  • array manipulation
  • callback function
  • key-based transformation
  • data processing
  • array transformation
  • utility function
  • data structure conversion

Contributing

We welcome contributions to this project. If you have a feature request, bug report, or want to improve documentation, please feel free to open an issue or submit a pull request.

Contribution Guidelines

  1. Fork the repository.
  2. Create a new branch: git checkout -b <branch_name>.
  3. Make your changes and commit them: git commit -m '<commit_message>'.
  4. Push your changes to the branch: git push origin <branch_name>.
  5. Submit a pull request.

License

This project is licensed under the MIT License.