diginext-utils
Version:
README.md
32 lines • 943 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.toObject = toObject;
/**
* Converts an array to an object using array values.
* If the input is already an object, returns it as-is.
* Each array element becomes a property with its index as the key.
*
* @template T - The type of values
* @param value - The value to convert (array or object)
* @returns An object representation
*
* @example
* ```ts
* toObject([1, 2, 3]); // { '0': 1, '1': 2, '2': 3 }
* toObject(['a', 'b', 'c']); // { '0': 'a', '1': 'b', '2': 'c' }
* toObject({ x: 1 }); // { x: 1 }
* ```
*/
function toObject(value) {
if (Array.isArray(value)) {
return value.reduce((acc, item, index) => {
acc[String(index)] = item;
return acc;
}, {});
}
if (typeof value === "object" && value !== null) {
return value;
}
return {};
}
//# sourceMappingURL=toObject.js.map