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.
install with Npm
npm install @abydin/convert-array-to-object
install with Yarn
yarn add @abydin/convert-array-to-object
Here are some examples of how to use the convertArrToObj
function:
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' ]
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.
- 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.
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' } }
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.
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' }
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.
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' ]
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
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.
- 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.
This project is licensed under the MIT License.