@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
Maintainers
Readme
convert-array-to-object
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 totrue
, the function returns an array of values instead of an object.
Returns
- If
reArr
isfalse
(default): Returns an object where keys are taken from the specifiedkey
and values are the result of the optionalcallBack
function. - If
reArr
istrue
: Returns an array of values derived from the specifiedkey
and optionalcallBack
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
- Fork the repository.
- Create a new branch:
git checkout -b <branch_name>
. - Make your changes and commit them:
git commit -m '<commit_message>'
. - Push your changes to the branch:
git push origin <branch_name>
. - Submit a pull request.
License
This project is licensed under the MIT License.