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-objectinstall with Yarn
yarn add @abydin/convert-array-to-objectHere 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
reArrisfalse(default): Returns an object where keys are taken from the specifiedkeyand values are the result of the optionalcallBackfunction. - If
reArristrue: Returns an array of values derived from the specifiedkeyand optionalcallBackfunction.
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-objarray conversionobject transformationdata manipulationJavaScript utilityarray to object mappingarray manipulationcallback functionkey-based transformationdata processingarray transformationutility functiondata 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.