I am trying to create an array to object, it’s working fine now.
But, there is a dependency in to the object { ids: [], items: {} }
Before I have declared the object with the required properties.
I am trying to achieve the same thing without the object dependency at the beginning,
initially it will be the {} not { ids: [], items: {} }
import { v4 as id } from "uuid";
export const createItem = (name) => {
return {
id: id(),
name,
packed: false
};
};
let items = ["Backpack", "Vitamins", "Kindle"].map((x) => createItem(x));
console.log(items);
const converter = items.reduce(
(obj, item) => {
obj.ids.push(item.id);
obj.items[item.id] = item;
return obj;
},
{ ids: [], items: {} }
);
console.log(converter);