Reputation: 35050
I have following array of tuple, need to make an object from, how?
Object.keys(invoiceItems)?.map(function (key, item) {
[key, 1];
}
I know in Swift Dictionary(uniqueKeysWithValues:
method call I need, what about in Typescript?
Upvotes: 0
Views: 1063
Reputation: 1539
Here I assume your invoice items with some dummy data, below are 2 ways to create object with key:value
const invoiceItems = [[101, 'Coffee'], [102, 'Tea'], [103, 'Book']]
// Method 1
const o = Object.fromEntries(invoiceItems);
console.log('o :', o)
// Method 2
const o2 = {}
Object.keys(invoiceItems).forEach(i => o2[invoiceItems[i][0]] = invoiceItems[i][1])
console.log('o2 :', o2)
Upvotes: 0
Reputation: 9873
Assuming that by tuples you mean two-element arrays, you can use Object.fromEntries
to create such object:
const entries = [
[ "value1", 42 ],
[ "value2", 17 ],
[ "value3", 51 ],
];
const object = Object.fromEntries(entries);
console.log(object);
Upvotes: 3